简体   繁体   中英

Javascript error message for rock, paper, scissors game

Please i need help with the code below. I have been getting this error message SyntaxError: expected expression, got keyword 'if' and I think I'm doing the right thing.

 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!"; } 

Content of functions in Javascript goes inside braces. So your current code

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

will become

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM