【问题标题】:Where is my Syntax Error? javascript我的语法错误在哪里? javascript
【发布时间】:2014-11-10 16:28:14
【问题描述】:
var userChoice = function(answer) 
{
    prompt("Do you choose rock, paper, or scissors?");
}
var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice is between 0 and 0.33) {
    computerChoice = "rock";
} else if (computerChoice is between 0.34 and 0.66) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

我明白了

SyntaxError: Unexpected Identifier

我的错误在哪里?

【问题讨论】:

  • javascript 中没有 is between 构造。
  • if (computerChoice > 0 && computerChoice < 0.33)
  • 您需要使用不等运算符()而不是“is between”和逻辑运算符而不是“and”(&&,||)
  • 出于好奇,是否有任何语言具有is between 运算符?
  • @rink.attendant.6 我所知道的最近的是 SQL 中的 BETWEEN 运算符。

标签: javascript if-statement


【解决方案1】:

没有“中间”运算符之类的东西。您需要像这样使用>=<=

var userChoice = function(answer) 
{
    prompt("Do you choose rock, paper, or scissors?");
}
var computerChoice = Math.random()
console.log(computerChoice)
if (computerChoice >= 0 && computerChoice <= 0.33) {
    computerChoice = "rock";
} else if (computerChoice >= 0.34 && computerChoice <= 0.66) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}

【讨论】:

    【解决方案2】:

    如果您要使用一种语言,请花一些时间查看语法。

    没有“是...和”结构。

    您必须像某些 cmets 指出的那样使用逻辑与:

    if (computerChoice > 0 && computerChoice < 0.33)
    

    【讨论】:

      【解决方案3】:

      无需测试下限。

      试试这个:

      var userChoice = function(answer) {
          prompt("Do you choose rock, paper, or scissors?");
      };
      
      userChoice();
      var computerChoice = Math.random();
      
      if (computerChoice < 0.33) {
          computerChoice = "rock";
      } else if (computerChoice < 0.66) {
          computerChoice = "paper";
      } else {
          computerChoice = "scissors";
      }
      console.log(computerChoice);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多