Rock, Paper, Scissors in JavaScript

前端 未结 13 1688
野的像风
野的像风 2020-12-03 00:13

I\'m working on making my first game (Rock Paper Sissors) and I ran into an issue where when the userChoice is scissors and the com

相关标签:
13条回答
  • 2020-12-03 01:03

    Example without all the {} and else if

    always use else if when you can.. since your if statements are different cases and only one applies you should use else if..

    with if statements if you only have 1 statement after the condition you don't need {} (condition 1 below).. even if you have a if.. else if...block statement its considered one statement..(condition 2 below).. but if it helps you can use them around the if.. else if...block statement to help your understand it better ..(condition 3 below).. ..

    also don't use === unless you really know what it does.. it can cause you trouble being a rookie.. use == by default..

    if(choice1 == choice2)  //condition 1
        return "The result is a tie!";
    else if(choice1 == "rock") //condition 2
        if(choice2 == "scissors") 
            return "rock wins";
         else 
            return "paper wins";
    else if(choice1 == "paper"){ //condition 3
        if(choice2 == "rock") 
            return "paper wins";
         else 
            return "scissors wins";
    }
    else if(choice1 == "scissors")
        if(choice2 == "rock")
           return "rock wins";
        else 
           return "scissors wins";
    
    0 讨论(0)
提交回复
热议问题