请我需要以下代码的帮助。
我一直在收到此错误消息SyntaxError:期望的表达式,得到了关键字'if',我认为我在做正确的事情。



var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
`enter code here`
if (computerChoice < 0.34) {
  computerChoice = "rock";
} else if (computerChoice <= 0.67) {
  computerChoice = "paper";
} else {
  computerChoice = "scissors";
}
console.log("Computer: " + computerChoice);
var compare = function(choice1, choice2)
if (choice1 === choice2) {
  return "The result is a tie!";
}

最佳答案

Javascript函数的内容放在花括号中。所以你当前的代码

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


会变成

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

关于javascript - 岩石,纸张,剪刀游戏的Javascript错误消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30391654/

10-12 05:37