Codecademy 错误(困惑)^^

Code academy error (confused) ^^

我想知道为什么此代码给出消息:SyntaxError: unexpected token else。

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 之后有一个 else if,所以它被绊倒了。

应该是

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

If 语句总是以 if 开头,然后是 else if,最后是 else。除了第一个 if 之外的所有内容都是可选的,但顺序必须始终相同。

因为else if应该else之前,像这样:

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