简体   繁体   English

Javascript 石头、纸、剪刀游戏

[英]Javascript Rock, Paper, Scissors game

I have been working on this Rochambeau game in JavaScript and so far when I run it, it will prompt for user input but no matter what you input, the result is ALWAYS a tie.我一直在用 JavaScript 开发这个 Rochambeau 游戏,到目前为止,当我运行它时,它会提示用户输入,但无论你输入什么,结果总是平局。 Here is my code:这是我的代码:

 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"; } console.log("Computer: " + computerChoice); function compare(userChoice, computerChoice){ if(userChoice === computerChoice){ alert("The result is a tie!"); } else if(computerChoice === "scissors"){ alert("rock wins!"); if(computerChoice === "paper"){ alert("paper wins!"); } } else if(computerChoice === "rock"){ alert("rock wins!"); if(computerChoice === "scissors"){ alert("scissor wins!"); } } else if(computerChoice === "rock"){ alert("rock wins!"); if(computerChoice === "paper"){ alert("scissors wins!"); } } } compare();

Any idea why this isn't running the rest of the conditions?知道为什么这不运行其余条件吗?

You're not passing any parameters to the function.您没有向函数传递任何参数。 The parameters in the function will default to undefined .函数中的参数将默认为undefined So undefined === undefined equals true.所以undefined === undefined等于 true。 You should do this instead:你应该这样做:

compare(userChoice, computerChoice);

compare();相比();

Aren't you missing the parameters?你不是缺少参数吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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