SyntaxError:意外的令牌if

时间:2015-06-18 15:51:13

标签: javascript if-statement token

我正在学习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";
        }

1 个答案:

答案 0 :(得分:0)

应该是:

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