Error, expected an identifier and instead saw ‘else’


#1

what if choice1 is paper

Cant seem to get my second else if statement correct
SyntaxError: Unexpected token 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

Your code and structure is fine its just the curly brackets that are wrong. Study mine and look at the differences.

var compare = function(choice1,choice2){
    if(choice1 === choice2){
        return"The result is a tie!";
    }
else if(choice1 === "rock"){
      if(choice === "scissors"){
         return "rock wins";
    }
    
    else{
        return "paper wins";
    }
}
    else if(choice1 === "paper"){
        if(choice2 === "rock"){
            return "paper wins";
        }
    }
        else{
            return "scissor wins";
        }

}

Let me know if you need further explanation. :slight_smile:
However If this got your head around it, please mark my reply as the solution.


#3

Thanks. I guess I couldn't see it at first after looking at it for an hour or so getting frustrated. I had been modifying bracket positions over and over not realizing I missed on in a very critical spot.


#4

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