【问题标题】:I'm having trouble with the syntax for my compare function for javascript我的 javascript 比较函数的语法有问题
【发布时间】:2013-05-28 23:32:32
【问题描述】:

我已经附上了我今天犯的 codeacademy 错误的屏幕截图。我正在尝试创建一个比较函数,该函数随机选择一个介于 0 和 1 之间的数字(纸、剪刀或石头),该数字输入两个选项并根据选项 1 与选项 2 的比较返回获胜者。

第一部分是注释,但它解释了原始的剪刀石头功能是如何构建的

代码如下:

/*var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
}*/

var compare = function (choice1, choice2) {if (choice1 === choice2) return("The result is a tie!");  
if (choice1 < 0.34) 
if(choice2 ==="scissors");
    return("rock wins");
} else if(choice2 ==="paper");{
    return("paper wins");
};    
};

它告诉我在第 15 行(else if 行)上有一个意外的标记。

当我删除 else 部分时,它给了我另一个语法错误,说明关于令牌 if 的相同内容。我被困在我的语法的哪一部分是关闭的以及如何修复它上。

【问题讨论】:

  • 与注释代码比较,找出额外使用的semicolons
  • 顺便说一句:如果choice1 是一个随机选择的数字,而choice2 是“剪刀”、“石头”或“纸”,那么您的if 语句if (choice1 === choice2) 永远不会是true。您应该选择一种格式(0 到 1 之间的数字或三个字符串之一)并以相同的格式将choice1 和choice2 都传递给您的比较函数。
  • 这是一个很好的观点。我以后会小心的。谢谢

标签: javascript function compare syntax-error


【解决方案1】:

我感觉这与===if() 语句之后的; 有关,无论哪种方式,这是比较它们的更好方法。

function compare(a,b)
{
    if(a==b)return "draw";
    switch(a)
    {
        case "rock":return (b=="scissors"?a:b)+" wins";
        case "paper":return (b=="rock"?a:b)+" wins";
        case "scissors":return (b=="paper"?a:b)+" wins";
    }
}
console.log(compare("scissors","paper"));

【讨论】:

  • 这可能会奏效,但由于我在本课的这一点上没有被告知如何使用案例,所以我犹豫是否要尝试。当我被要求使用用例时,我仍然会使用这种语法作为参考。感谢艾萨克的帮助。
  • 没问题,switchcase 是为数不多的 goto 类型的语句之一,没有人会因为你使用而皱眉。
【解决方案2】:
function compare(choice1, choice2) {
  if (choice1 === choice2) {
    return "The result is a tie!";
  }

  if (choice1 < 0.34) {
    if (choice2 === "scissors") {
      return "rock wins";
    } else if (choice2 === "paper") {
      return "paper wins";
    }
  }
}

【讨论】:

  • 感谢您的帮助 greg84。
【解决方案3】:

检查下面与分号相关的错误。

var compare = function (choice1, choice2) {
  if (choice1 === choice2) return("The result is a tie!");  

  if (choice1 < 0.34) {

    if(choice2 === "scissors") { // remove ; here
      return("rock wins");
    } else if (choice2 === "paper") { // remove ; here
      return("paper wins");
    } // remove ; here

  } // add another else => what happens when choice1 >= 0.34 (not a rock)
};

使用所需的 else 块,完整的函数如下所示:

var compare = function (choice1, choice2) {
  if (choice1 === choice2) return("The result is a tie!");  

  if (choice1 < 0.34) { // rock

    if(choice2 === "scissors") {
      return("rock wins");
    } else if (choice2 === "paper") {
      return("paper wins");
    }

  } else if (choice <= 0.67) { // paper

    if(choice2 === "rock") {
      return("paper wins");
    } else if (choice2 === "scissors") {
      return("scissors wins");
    }

  } else { // scissors

    if(choice2 === "paper") {
      return("scissors wins");
    } else if (choice2 === "rock") {
      return("rock wins");
    }

  }
};

编辑
这只是为了帮助您克服对分号的困惑(如果有的话)。通常,函数定义不需要在其主体通过放置最后一个右花括号} 完成后具有;

function compare (choice1, choice2) {
  // ...
}

相反,当我们给变量赋值时,语句以分号结束。

var name = "John Doe";

因此,当我们将两者结合起来时,我们定义了一个函数,然后在需要使用分号关闭的赋值语句中使用它。因此,语法:

var compare = function (choice2, choice2) {
    // ...
};

【讨论】:

  • 这段代码运行正常。我会更多地研究它,但我喜欢你如何构建它并分离每个条件以帮助我在视觉上遵循。我特别发现在 if/else 语句中包含 cmets 有助于组织代码。也许这是您指定每个可能结果的方式(包括选择 1 的不同场景)。我不知道我是否提到它应该是一个给定的选择 1 是摇滚。 (说明中提到过)。非常感谢你的帮助。我以后会参考这种格式。
  • 检查我对何时使用分号的编辑,如果它是最有帮助的答案,请考虑接受。
猜你喜欢
  • 2021-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多