Codecademy语法错误意外的标记else

Codecademy Syntax Error unexpected token else

本文关键字:else 意外 语法 错误 Codecademy      更新时间:2023-09-26

所以我正在学习JS codecademy课程,我正在学习石头剪刀,我还看过其他主题,但据我所知,我没有放错分号。你能告诉我怎么了吗?

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

看起来你把大括号搞砸了:

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

您有一个else,后面跟一个else-if:

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