【发布时间】:2019-08-06 15:33:53
【问题描述】:
我目前正在用 JavaScript 编写石头剪刀布游戏,出于某种原因,无论玩家输入什么,我总是得到“平局”结果。在过去的一个小时里,我一直在试图弄清楚,但没有骰子。非常感谢任何帮助。我已经把我的代码放在下面了。
let computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
let playerPrompt = prompt("Rock, paper, or scissors?")
let playerChoice = String(playerPrompt).toLowerCase
function playRound(playerChoice, computerChoice) {
if (playerChoice === computerChoice) {
return "Draw!"
} else if (playerChoice === "rock" && computerChoice === "scissors") {
return "Player wins!"
} else if (playerChoice === "paper" && computerChoice === "rock") {
return "Player wins!"
} else if (playerChoice === "scissors" && computerChoice === "paper") {
return "Player wins!"
} else {
return "Computer wins!"
}
}
let results = playRound()
console.log(results)```
【问题讨论】:
-
你没有把参数传给函数!
-
你需要做一些基本的调试。使用
console.log找出playerChoice和computerChoice实际上是什么。您在调用它时忘记将它们传递给playRound,并且您将playerChoice设置为toLowerCase函数,而不是调用该函数并获取返回值。您的问题基本上是拼写错误的集合。
标签: javascript