【问题标题】:SyntaxError: Unexpected token ifSyntaxError:意外的令牌如果
【发布时间】:2015-09-04 08:59:23
【问题描述】:
我目前正在学习 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";
}
【问题讨论】:
标签:
javascript
if-statement
token
【解决方案1】:
应该是:
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"
}
}
【讨论】:
-
@BrandonMagro 请检查Tour。如果你的问题解决了,你可以标记正确的答案。