7/9 Syntax error: Unexpected token else


#1

Looked through a number of similar threads and can't seem to relate any of them to my own error! Any ideas?

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

At first glance, there's one more closing bracket you don't need. After the return "rock wins" row and return "paper wins" row. Right now, the else if blocks get closed before the else that's inside them gets closed. Format your code like that and you should see the problems at glance: Opening brackets on the same row and closing brackets with the same indentation like the block they're closing.

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

Here's your example reformatted and fixed. (Look through the opening and closing brackets) I hope that helped!


#3

Perfect thank you very much!


#4

Thank you!! You really saved my day!!