SyntaxError: Unexpected token else


#1

Can someone help me plz? I've gone through everything and checked every other post and nothing's helping. Here's the code that's having problems;

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

else
{
if (choice2 === "paper":wink:
{return "scissors wins";}
else
{return "rock wins";}
}
}

thanks :smiley:


#2

You have semicolons scattered around your conditions that aren't needed.


#3

Can you point out which ones exactly plz? Thanks so much


#4

I'm having the exact problem as well. I've even tried directly copy / pasted and it did not work.


#5

Also I believe you've written the code incorrectly, many of your curly brackets are in incorrect positions (these -> { } ).


#6

I can't point out all of them, but remember, if/else if/else syntax is:

if (condition) {
    Statements;
    Statements;
}

#7

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

I had the same issue and I fixed the braces ' { ' after the if statement and it worked.