javascript - 只在石头剪刀布游戏中获得“平局”结果
问题描述
我目前正在用 Javascript 编写 Rock、Paper、Scissors 游戏,出于某种原因,无论玩家输入什么,我总是得到“平局”结果。在过去的一个小时里,我一直在试图弄清楚,但没有骰子。非常感谢任何帮助。我把我的代码放在下面。
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)```
解决方案
如果我没记错的话,你没有将任何参数传递给playRound()
,它可能应该是:
let results = playRound(playerChoice, computerChoice)
编辑:正如 Quentin(和 Alon Eitan)所提到的,这不是唯一的问题:
let playerChoice = String(playerPrompt).toLowerCase
实际上将函数分配String.toLowerCase
给playerChoice
,如果你想要语法的小写值playerPrompt
应该是
let playerChoice = playerPrompt.toLowerCase()
或直接
let playerChoice = prompt("Rock, paper, or scissors?").toLowerCase()
推荐阅读
- c++ - 为什么在 C++(Mac 上)中使用 & 指向 Struct 不正确?
- android - 如何通过 Motion Layout 缩放将 RecyclerView 项目设置为片段
- javascript - 是否可以使用 REST API 从 Nuget 私人商店获取包详细信息?
- html - 折叠导航栏中浮动元素的边距
- javascript - 如何过滤不同键值的 json 数据并使用该键值创建新的 json 元素,而无需硬编码数据
- python - Pandas 数据框行到列表的字典,使用每行的第一个值作为键
- java - 如何在片段中使用 ViewModel?
- c++ - 寻找洞察为什么我在这里遇到访问冲突
- ios - 将数据从 json 模型传输到数组
- qt - 如何将 QTextToSpeech(任何 Qt 类)导出到 QML?