Rock paper scissors game


#1

getting an error on step 7 of the rock paper scissors game, like pretty much everyone else, haha. unexpected token else is the error message I am getting, I am pretty sure has to do with the way I am nesting my second else if statement, but I am not sure.

var compare = function(choice1, choice2)
{
    if(choice1 === choice2){
return"The result is a tie!";
}

    else if(choice1 ==="rock")
    {

    if(choice2 === "scissors")
    return "rock wins";
}
    else 
    {
    return "paper wins"
    }
    else if(choice1 ==="paper")
    {
        if(choice2 ==="rock")
        return "paper wins";
    }
    else
    {
        return "scissors wins"
    }
};


#2

Even your first else if statement should have another closing bracket after this line,

And all you need to fix your code is three closing curly brackets at the end like so,

   }
} 
};

#3

I added another closing bracket after return "paper wins" and made sure there were three closing brackets at the very end. still no luck, do you want me to re-paste code?


#4

Yes post your updated code please :slight_smile:


#5

var compare = function(choice1, choice2)
{
if(choice1 === choice2){
return"The result is a tie!";
}

else if(choice1 ==="rock")
{

if(choice2 === "scissors")
return "rock wins";

}
else
{
return "paper wins"
}
else if(choice1 ==="paper")
{
if(choice2 ==="rock")
return "paper wins";}
}
else
{
return "scissors wins"
}
}
};


#6

You have no opening curly brackets after this statement. {

You need two brackets after this line,

like so,

else 
{
return "paper wins"
}
}'

And the same for here, opening curly bracket after the if statement. {


#7

Awesome! I got it, thanks so much. This isn't urgent, but I want to better understand the opening and closing brackets, I have commented and numbered each bracket, which closing bracket corresponds to each opening bracket. Could somebody confirm that what I have is correct?

var compare = function(choice1, choice2)
{ /*opens function */
if(choice1 === choice2){ /* #1 */
return"The result is a tie!";
} /* #1 */
/* round 1 */
else if(choice1 ==="rock")
{ /* #2 */

if(choice2 === "scissors"){ /* #3 */
return "rock wins";

} /* #3 */
else
{ /* #4 */
return "paper wins"
} /* #4 */
} /* #2 */
/*here is the break */
else if(choice1 ==="paper")
{ /* #5 */
if(choice2 ==="rock"){ /* #6 */
return "paper wins";

} /* #6 */
else
{ /* #7 */
    return "scissors wins"
} /* #7 */

} /* #5 */
}; /* closes function */


#8

That is a bit more complicated to understanding when to open and close brackets. Here's something to have at the back of your mind anytime your dealing with brackets.

if statement -- if (condition) **{** /Bracket after the condition.

else statement --- else **{** /Bracket after else.

`functions -- var myfunc = function(x, y) **{** /Bracket after parentheses
containing arguments or nothing. ()

#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.