javascript 语法错误:如果出现意外标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30920148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:56:18  来源:igfitidea点击:

SyntaxError: Unexpected token if

javascriptif-statementtoken

提问by Brandon Magro

I'm currently learning javascript and I keep having this error!!!

我目前正在学习 javascript 并且我一直有这个错误!!!

This is my script:

这是我的脚本:

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

采纳答案by Walter Chapilliquen - wZVanG

Should be:

应该:

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

Or neater:

或者更整洁:

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