8/9 SyntaxError: expected expression, got keyword 'else' HELP


#1

function compare(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";
}
else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
}
};

There's just something that won't work! Can someone plz help.


#2

Try this.

function compare(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";
        }
    }

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

#3

Thx, but is it possible that you can explain what was wrong in my code? I'd really appreciate it.


#4

No problem. If you take a look you can break your code down

function compare(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!";} //closes the if

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

Everything is fine up to here

then we have

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

The first else if in this block should actually be closed further up

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

everything else is fine

else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
} //this closes the else if
// we removed the old }
}; // this closes the function

You might have gotten tripped up since in your first else if block (the one that checks for rock) you didn't enclose the

else return "paper wins"; in brackets {}

which is fine but you did enclose in {} with the two other ones


#5

Now I understand. Thank you very much!


#6

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";
}
}
}; else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock wins";
}
else {
return "scissors wins";
}
}
};

can anyone help? I keep getting keyword else also