Syntax Error "Unexpected token" 7/9 Rock Paper Scissors


#1

Can someone look at my code and see what I am missing. I think it may be a curly bracket somewhere?

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

You are right! :smile:

This is your 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";
        } else if (choice1 === "paper") {
            if (choice2 === "rock") {
                return "paper wins";
            } else {
                return "scissors wins";
            }
        }
    }
};

Do you see? Second else if is nested inside your first else if. You have to add } before it and then you have to delete one of the } from the end of the script:

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

The reason of the error message is that else block ends your if statement, you can't add next else if after else.


#3

Please help too!! This is my code & I can't find the syntax error: expected expression, got keyword '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 === "scissors")
            {
                return "paper wins";
            }
        }
        else{
            return "scissors wins";
        }
};

Thank you:)


#4

Reason of the problem is unwanted } character. You just have to delete it. I marked it with a comment, take a look:

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

This } was misleading, because next } characters ended your if ... else and your function.


#5

It would really help if you could keep some formatting like 1 tab indentation for each level of nestig if I throw your code in http://jsbeautifier.org/ I can see that your function ends to early:

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";
    }
}/* here does your function end*/ 
// so this else is not connected to an if any longer

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

#6

Thank you so much! It sorted my problem out and gave me the green light.


#7

Thank you! I will use "the beautifier" in the future :smile:


#8

You're very welcome!


#9

This actually helped me too! I was missing a bracket separating the second else if statement from the first else if statement - thank you!!


#10

Oh this is great, thanks everyone for your help!
Sometimes it's the finest details that can derail the whole thing!


#11

Thank you for letting me know, I'm really happy that it's helpful :smile:

You're very welcome.