6/9 "Unexpected Token else"


#1

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

I have been trying to fix this issue for awhile now and I keep getting the same error. For some reason the else if function is invalid and I don't know why. The code that I have written seems perfect, but can someone please explain to me what I am doing wrong. Thank you.


#2

Put your if statement above the else if statement.

if
else if
...
else

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


#3

I tried, but unfortunately I get illegal return statement.


#4

Oh...sorry. To use return you must be inside of a function. Only functions can "return" a value. Just use console.log("").


#5

Could you post the rest of the code as well?


#6

@digitalrockstar92867,
Have a read through
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on

  • the build of the IF ELSE-IF statement structure ( do not forget closing-curly-bracket-} at the end, which is used to close the =compare= function )

#7

Should it be choice 1 === "rock" instead of "rocks"?


#8

var compare = function (choice1, choice2) {
if (choice1 === choice2) {
It will help you

return "The result is a tie!"
}
else if (choice1 === "rock") {
if (choice2 === "scissors")
return "rock wins";
}
else {
return "paper wins"
}
}


#9

@megamaster20225,
You are forgetting a closing curly-bracket-}
at the end....