收到“SyntaxError:Unexpected token'{'。Parse error”

时间:2015-12-14 23:45:41

标签: javascript

var userChoice = prompt("Do you choose rock, paper or scissors");

var computerChoice = Math.random(); 
if (computerChoice <= .33) {
    computerChoice === "rock";
}
else if (computerChoice <=.66){
computerChoice === "paper";
}
else (computerChoice <= 1) {
    computerChoice === "scissors";
};
console.log(computerChoice);    

接收

  

SyntaxError:意外的标记'{'。解析错误

2 个答案:

答案 0 :(得分:2)

你不应该为else添加条件。

else (computerChoice <= 1) {
    computerChoice === "scissors";
};

应该是

else {
    computerChoice === "scissors";
};

或者,如果您需要条件,请使用else if代替else

else if (computerChoice <= 1){
    computerChoice === "scissors";
};

请记住,要为变量赋值,只需一个=即可。 =====用于比较。

因此,请将===替换为=

答案 1 :(得分:1)

if / else语句的else部分没有条件,所以你应该在最后一个条件之前有一个if语句。

if (computerChoice <= .33) {
    computerChoice === "rock";
}
else if (computerChoice <=.66){
computerChoice === "paper";
}
//   \/
else if (computerChoice <= 1) {
    computerChoice === "scissors";
}