What's the syntax error in the following code?


#1

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

This is your code with proper formatting, take a look at my comment (text after //):

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") { // -- > this else if is misplaced
            if (choice2 === "rock") {
                return "paper wins";
            } else {
                return "scissors wins";
            }
        }
    }
}

And this is the cause of the error. else if cannot be preceded by else statement. else statement always ends if ... else if ... else construction.


#3

So how should I construct my code if that is the issue? (It still says syntax error when I copy and paste your new code)


#4

Code in my comment is yours, I just formatted it.

If you do not know how to fix this error you probably should get back to exercises about if ... else. You just have to close one else if before starting next one.