What if choice1 is paper, Syntax error, expected expression, got keyword 'else'


#1

I have trouble with my code, getting that syntax error even though I think I did everything right, help please!

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

Hi @ali_sahmoud,

Please see the code sample below. Although it is only preference, essentially, it helps to add a little code style formatting when you write up any long code; this can help to follow the code and spot any errors :slightly_smiling:In this case you were visited by the paren gremlins.

compare = function(choice1, choice2) {
    if (choice1 === choice2) {
        return "The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") { //missing opening paren
            return "rock wins";
        }
        else {
            return "paper wins";
        }
    } //missing closing paren
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
};

#3

Got it thanks a lot! I really appreciate it!