7.SyntaxError: Unexpected token else


#1


Hi guys. I cant figure it out whats wrong with the code :frowning: Any suggestions?


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


#2

It looks like you're missing some steps, but if you're just referring to the error "Unexpected token else," it's because you're if/else sequence is off:

function compare (userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
}
else if (computerChoice ==="scissors") {
return "rock wins";
}
else { //here, you ended the if/else block by just saying "else", not "else if"
return "paper wins";
}
else if (userChoice === "paper") { //then here, you went back to "else if"
if (computerChoice === "rock") {
return "paper wins";
}
else {
return "scissors wins";
}
}
};


#3

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