What if choice1 is paper? Syntax error: unexpected token else (please help)


#1


https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/3?curriculum_id=506324b3a7dffd00020bf661#


Hey! There is a problem here with my syntax, but i just can´t figure it out.
Ive been trying to solve it for days! But i just cant find whats wrong with it...
Please help me if you can, and have a nice day!

Syntax error: 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

are you sure this is your whole code? there should be a prompt for the userInput and a random number generator for the compChoice


#3

needs another close brace here.


#4

The early part doesn't have a problem with the syntax, its just this part that has a problem...

Is it supposed to look like this?
Because it still doesn't want to work for me...

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

Thanks for helping me out!


#5

so is it working yet?


#6

} <------ this curly brace needs to move to
} else {
return "paper wins";
<------Here
} else if(choice1 === "paper") {


#7

It worked! :smiley:
Thank you so much man!!!
This is what it looks like right now:

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

Thanks again, and have a nice day!


#8

hey I just woke up, sorry I missed the message. anyway, its no problem. good luck with the course :smile:


#9

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