javascript - 在提示中输入石头、纸或剪刀后在控制台中未定义
问题描述
<script>
console.log("enter \game()\ to start the game");
function computerPlay(){
let a = ['rock','paper','scissors'];
let b = a[Math.floor(Math.random * a.length)];
return b;
}
function humanPlay(){
let c = prompt('rock,paper or scissors');
return c;
}
function playRound ( computerSelection,humanSelection){
if (computerSelection == humanSelection) {
console.log("Its a tie");
}
if (computerSelection == 'rock' && humanSelection.toLowerCase() == 'paper') {
return 'human wins';
}
else if (computerSelection == 'rock' && humanSelection.toLowerCase() == 'scissors') {
return 'computer wins';
}
else if ( computerSelection == 'paper' && humanSelection.toLowerCase() == 'rock') {
return 'computer wins';
}
else if (computerSelection == 'paper' && humanSelection.toLowerCase() == 'scissors') {
return 'human wins';
}
else if (computerSelection == 'scissors' && humanSelection.toLowerCase() == 'rock') {
return 'human wins';
}
else if (computerSelection == 'scissors' && humanSelection.toLowerCase() == 'paper') {
return 'computer wins'
}
else if (computerSelection == 'rock' && humanSelection.toLowerCase() == 'scissors') {
return 'computer wins';
}
}
function game(){
computerSelection = computerPlay();
humanSelection = humanPlay();
let results = playRound(computerSelection,humanSelection);
console.log(results);
}
</script>
我一直在尝试从 Odin 项目编写这个简单的游戏 2 小时,但我无法理解为什么代码不起作用。我查看了一些学生的解决方案,并尝试一次又一次地编写我的代码,但似乎每次都不起作用。我在这个脚本中做错了什么?
解决方案
我认为您的问题出在这一行:
let b = a[Math.floor(Math.random * a.length)];
它应该是:
let b = a[Math.floor(Math.random() * a.length)];
此外,您可以在此处测试您的代码:https ://js.do/
希望能帮助到你!
推荐阅读
- c - 使用快速排序对带有指针的数组进行排序
- amazon-web-services - AWS ECS Fargate 任务无法从 Cloudformation 堆栈正常工作
- webgl - 启用隐藏表面移除的意外绘制顺序
- python - 多索引 Pandas 数据帧上的布尔索引仅对具有空值的行进行子集化
- javascript - 如何压缩本地文件夹并下载它?
- java - 如何执行终端PITest?
- android-studio - 如何在 Android Studio 中更改 AVD 中的虚拟电话
- chart.js - Chart.js 时间线
- sql-server - 日期范围内每组的最小值
- flask-wtforms - WTForms - 是否有对象字段?