Why doesn't my second 'else if' statement work?


#1

I keep getting the Syntaxerror: 'expected expression, got keyword else'
The first ' else if' function worked fine, but when I copy the structure for the second 'else if' it doesn't work?

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

Can somebody tell me what I'm doing wrong. Because I can't figure it out.


#3

Bracket placement is the cause of the problems:

var compare = function(choice1, choice2) {
     if (choice1 === choice2) {
        return"The result is a tie!";
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
        }
    }     // this brace
        else { 
            return "paper wins";
        }
    // should be here
    else if (choice1 === "paper") {
        if choice2 === "rock") {
            return "paper wins";
        }
    }     // this brace
        else {
            return "scissors wins";
        }
    // should be here
};

#5

I misread your comment the first time. I tried again and found out you were right and I misplaced the brace!
I did it like this:

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


#6

Finally! I've been looking for a correct answer for AGES!