语法错误无法查明 javascript 中的错误

syntax error can't pinpoint the bug in javascript

我不断收到语法错误,无法弄清楚原因,请帮忙。

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)

此结构在 javascript

中不存在
0.34 >= computerChoice < 0.67

无法表达这样的范围。您需要将其替换为

computerChoice >= 0.34 && computerChoice < 0.67

同样适用于

0.67 >= computerChoice <= 1
  1. 0.34 >= computerChoice < 0.67 在 JavaScript 中无效。请改用 computerChoice >= 0.34 && computerChoice < 0.67 之类的内容。

  2. 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);

Working Fiddle