Code Syntax Error?


#1

Hi, I was wondering if you saw any errors in the "else if (choice 1 === "paper")" section. Whenever I try to run it, the editor gives me back, "SyntaxError: expected expression, got keyword 'else'" and the popup box says, "Oops, try again. There was a problem with your syntax.

Here's the code:

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

It may be because there are if loops, else if loops, and else loops.

This is what you put:

If loop

Else if loop

Else if loop

(No else loop)

Maybe that's the problem?


#3

@neptinizer101 here is your code I have commented where the error was

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.";}
                }// you were missing this closing brace
                else if (choice1 === "paper") {
                    if (choice2 === "rock") {return "Paper wins.";}
                else {return "scissors wins.";}
                }
            };

#4

Right. I totally knew that. :smiley: