我目前正在学习javascript,并且持续出现此错误!!!

这是我的脚本:

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

最佳答案

应该:

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

或更整洁:
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"
    }
}

07-24 16:01