我继续收到语法错误,无法弄清楚原因,请帮忙。
alert ("CAN YOU BEAT VALERIE AT ROCK PAPER SCISSORS?");
var userChoise = prompt ("Rock, Paper, Scissors");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if (0.34 >= computerChoice < 0.67) {
computerChoice = "paper";
}
else (0.67 >= computerChoice <= 1) {
computerChoice = "scissors";
}
console.log("Valerie Dam picks" + " " + computerChoice);
Chrome控制台会引发以下语法错误:
Uncaught SyntaxError: Unexpected token { at Object.InjectedScript._evaluateOn (<anonymous>:895:140) at Object.InjectedScript._evaluateAndWrap (<anonymous>:828:34) at Object.InjectedScript.evaluate (<anonymous>:694:21)
答案 0 :(得分:0)
这种结构在javascript
中不存在0.34 >= computerChoice < 0.67
无法表达这样的范围。你需要用
替换它computerChoice >= 0.34 && computerChoice < 0.67
同样适用于
0.67 >= computerChoice <= 1
答案 1 :(得分:-1)
0.34 >= computerChoice < 0.67
在JavaScript中无效。请改用computerChoice >= 0.34 && computerChoice < 0.67
之类的内容。
else
[else (0.67 >= computerChoice <= 1)
...]的最后一个块应为else if
。
所以你纠正的代码应该是这样的:
alert ("CAN YOU BEAT VALERIE AT ROCK PAPER SCISSORS?");
var userChoise = prompt ("Rock, Paper, Scissors");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if (computerChoice >= 0.34 && computerChoice < 0.67) {
computerChoice = "paper";
}
else if (computerChoice >= 0.67 && computerChoice <= 1) {
computerChoice = "scissors";
}
console.log("Valerie Dam picks" + " " + computerChoice);