6. What if choice1 is rock?


#1

I am at: 6. What if choice1 is rock?

I get this error:
SyntaxError: Unexpected token else

and this is my code.

var compare = function(choice1, choice2){
    if (choice1 === choice2){
        return "The result is a tie!"
    }

    else if (choice1 === "rock" & choice2 === "scissors"){
        return "rock wins!";
    }
    else{
        return "paper wins";
    }
    else if (choice1 === "scissors" & choice2 === "paper"){
        return "scissors wins!";
    }
       else{
        return "rock wins";
    }
    
    else if (choice1 === "paper" & choice2 === "rock"){
        return "paper wins!"
    }
       else{
        return "scissors wins";
    }
}

#2

Not that I've formatted your code (always recommended) it is easy to see the problem.

If statements have three components:

if else ifandelse.

You always have to start with an if .
The last statement always has to be an else.
In Between you can have as many else ifs as you want.

Looking at your code you go from an else to an else if. This is not allowed, else finishes the group of statements so you can not go to an else if.

Try looking at your code and reorganizing it to fit these rules.

Hope it helps!


#3

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