If vs else if


#1


7. What if choice1 is paper?


After defining the option of a tie (choice1 === choice2), I feel that the rest of the if statements should be else if statements because choice1 could meet the conditions of the tie statement, yet continue down the code to look for other conditions it meets. The error resolved by replacing the else if statement for condition1, but my question is why does else if not also work. Thank you for your time in helping me with this.

SyntaxError: Unexpected token else.



#2

you didn't include any code? Please include code so we can help you


#3

I apologize for that. The 'if (choice1 === "rock") {' line is where I don't understand why an 'else if' statement couldn't be used. This is the code once I made that correction as per the SyntaxError message.

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

#4

because of the semi-colons, after the closing curly bracket of if, else if and else you shouldn't put a semi-colon after the }, only for the function. if you want to put semi-colons, you can put them after the return:

return("rock wins:);

#5

thank you for your help stetim94! I really appreciate it


#6