Unexpected token else


I cannot for the life of me understand what I did wrong. the first "else if" worked fine, then I copied it and replaced the words for the second "else if" and suddenly I have a syntax error. What's going on?


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


There are a few issues.

What is the syntax for an if or else if statement?
For an else statement?
What does a semicolon do in JavaScript?

Keep these in mind while reviewing your code.


The only change I could think of was adding these brackets:

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


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

An else statement has no condition. A semicolon is like a period in writing. I still don't know what I'm doing wrong.


Keep in mind, the semicolon is like a stop sign in JavaScript. Now, the ones you have inside your if, else if and else statements are contained by curly braces. These won't interfere with the compare function they are inside of. Take a look at the semicolons outside those code blocks. Would they affect your compare function?


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