Would someone be so kind as to help me find the error in my code!


#1

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

For some reason it says syntax error: unexpected keyword 'else'
I have no idea where I am going wrong.


#2

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"
    } 
} //Remove this curly bracket
    else if(choice1==="paper") {
        if(choice2==="rock") { 
            return "paper wins"; 
        }
        else {
            return "scissors wins";
        }
}
//Add a curly bracket here.

#3

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

I just did as you said and console says the same error is occurring :frowning:


#4

You used to have a function in front of your else, can you see that?
What do you have in front of that else now?

It should be an if-statement, but it's something else.


#5

I never had a function in front of else { return "scissors wins"; }
I don't think that is necessary because the return value scissors wins is the only other option that the function can run. I compared the bottom half of the code to the top half and it is identical, there was no issue with the syntax of the code in the previous lesson, that's why I am so confused at the moment.

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

looks like an if-statement doesn't it? Hmm, thanks for taking your time in discussing this with me I really do appreciate your input.


#6

You did have function( .. ) { .. } else

This is more clear in @jibblyj's post, where the indentation matches the curly braces.

Keep in mind that you have multiple else's, not just that one.

And now, you've got else { .. } else