Syntax Error: Unexpected Keyword 'else'?


#1

Hi there, not understanding why I see this error: unexpected keyword 'else'?
Anyone have any ideas? I tried refresh as well..

My code:

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

}


#2

Hi dthden66,

You closed your function too early.

var compare = function(choice1, choice2) {
if (choice1 === choice2) return "The result is a tie!";
} <----closed the function, remove this curly brace

else if (choice1 === "rock"){
if(choice2 === "scissors") {
return "rock wins";
} <----missing one more curly brace to close the else if condition

else {
return "paper wins";
}

} <----add a semicolon

It should work after this.


#3

This is your whole function at least according to opening and closing {}. So as the if is inside the function but the else (if) is not it is complaining because they are no longer connected. So better put the rest inside the function as well.


#4

Thanks so much! after making modifications code ran just fine!!!