Unexpected token else


#1

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?

https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661#


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


#2

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.


#3

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

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

and

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.


#4

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?


#5

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