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

时间:2015-06-11 02:55:01

标签: javascript syntax-error

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

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)

2 个答案:

答案 0 :(得分:0)

这种结构在javascript

中不存在
0.34 >= computerChoice < 0.67

无法表达这样的范围。你需要用

替换它
computerChoice >= 0.34 && computerChoice < 0.67

同样适用于

0.67 >= computerChoice <= 1

答案 1 :(得分:-1)

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

  2. else [else (0.67 >= computerChoice <= 1) ...]的最后一个块应为else if

  3. 所以你纠正的代码应该是这样的:

    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