7 unexpected token "else"


#1

for the love of god what am i doing wrong? its giving me an unexpected token error that is else
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";
}

}


#2

Tell me what's wrong here:


#3

The structure for an if, else if, else is the following.

if (value1 ==value2) {
//your code;
} else if (value1 == value2) {
//your code
} else if (value1 == value2) {
//your code. You can include as many else if as you want.
} else {
//your code
}

Remember that the 'if, else if, else' statement must always end with an else. That's why you get that error. Try changing the code so it includes it. Hope it helps :smile:


#4

Partially correct, yes this is how an if/else if/else statement could look like, but the else is not mandatory and the conditions may have more diversity than == e.g. value > value, value < value, variable != value aso as long as they evaluate to a boolean value.

The actual problem is probably what @ragezapper already pointed out and an explanation why this is a problem and what is happening can be found here:


#5

Correct. I didn't know you could really skip the else. And about the conditions, I knew that but I didn't know how to express it in a short way without putting all of them. :smile:


#6

I just often use "condition" when I need a placeholder and hope that pseudo-code is understandable.


#7

I was having the same problem and i's really annoying, but it looks like that I've forgot to close the function } so did u.


#8

Alright guys I appreciate all the help! :grinning: :


#9

else if(choice1==="paper"){
if(choice2==="rock"){
return "paper wins";
}
else{
return "Scissors win";
}

    }

}(brace for the function)