Unexpected Token Else - What if choice 1 is Paper


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

I cant seem to figure out why i still get the error message Unexpected token else. 
Any help would be appreciated.


#2

The semicolon is prematurely ending your compare function.


#3

I remove the semicolon and I'm still getting the same error message.

}

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

}


#4

I noticed that your "if" statements don't have opening curly brackets. So if(choice2 === "rock") { enter code}
I see this on both else if's.


#5

Interestingly enough I removed the curly bracket on the if statement on 6/9 and it went through. I played around with adding it/removing it to the if statement on 7/9 with no luck.

Any thoughts?


#6

Not sure why your nested if runs without brackets. What I've noticed is your code won't pass unless your syntax is correct. If you use a bracket at the start of your compare function, make sure you have a closing bracket at the end.


#7

Okay I did a bit of playing around. I took your code and on line 23, you need to remove that semi-colon. biff75 was correct. After that you need to add opening curly brackets to your if statements. The program isn't expecting the "else" statement because it thinks the if statement still being defined. Lastly, you need to add one closing curly bracket to line 32. Without it the program still thinks you're writing your last "else if" statement.


#8

Okay I did a bit of playing around. I took your code and on line 23, you need to remove that semi-colon. biff75 was correct. After that you need to add opening curly brackets to your if statements. The program isn't expecting the "else" statement because it thinks the if statement still being defined. Lastly, you need to add one closing curly bracket to line 32. Without it the program still thinks you're writing your last "else if" statement.

Thanks for the help! That worked!!!


#9

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