What if choice1 is paper?


#1



What's wrong with this code, can anyone help me?


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";
}
}


SyntaxError: Unexpected token else



#2

There's an extra closing bracket here that doesn't need to be there.


#3

You should add one more curly brace at the end and a semi-colon along with that rather than here:

Re-written:

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";
                }
            }
            };

Hope this helps!


#4

This worked, thanks!


#5

You're welcome.

Have fun coding! :slight_smile:


#6

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