javascript - 试图让我的第一个 JavaScript 项目工作,完整的功能显示在“控制台”中,而不是我想要的代码的实际结果
问题描述
我正在做我的第一个 JS 项目,它是一个简单的 Rock、Paper、Scissor 游戏,应该可以在控制台中运行。
我认为我的代码是非常正确的,但是当我“console.log”我的函数(playRound)时,我在“console”中得到以下片段:“你们都玩了 undefined It's a Draw!”。我不确定我错过了什么,所以如果有人可以看看,那将非常感激!
这是我的脚本:
function computerPlay (){
const choices = ["Rock", "Paper", "Scissor"];
const randomChoice = choices[Math.floor(Math.random() * choices.length)];
return(randomChoice);
}
console.log(computerPlay()) //just to show "computerPlay" works
function playRound(playerSelection, computerPlay) { //This is the actual round
if (playerSelection == computerPlay) {
result = `You both played ${computerPlay} It's a Draw!`;
} else if ((playerSelection == "Paper" && computerPlay == "Rock") ||
(playerSelection == "Rock" && computerPlay == "Scissor") ||
(playerSelection == "Scissor" && computerPlay == "Paper")) {
result = `${playerSelection} beats ${computerSelection} You Win!`;
} else {
result = `${computerPlay} beats ${playerSelection} You Loose!`;
}
return (result);
}
let playerSelection = 'rock'
console.log(playRound()) // to test if "playRound" works
感谢您的检查!
亲切的问候,编码新手
解决方案
定义函数后,应使用参数调用它以查看结果。playRound 函数有 2 个参数,请替换console.log(playRound)
为console.log(playRound(playerSelection, computerPlay()))
.
推荐阅读
- java - 在画布内画线
- javascript - 点击功能淡出和淡出。此功能在另一个 img 之上显示 img,但从不匹配屏幕大小
- ios - 滚动视图栏滚动,而内容视图不滚动
- java - 从 mainActivity 调用方法时出现 java.io.IOException(在类中调用时有效)(android studio)
- javascript - Javascript - 如何检查正则表达式以确保字符串包含至少一个字母/数字序列?
- php - 如何将 jQuery 过滤器添加到 html 表
- r - 如何解释列联表?
- topic-modeling - 主题建模:LDA 和 BTM
- javascript - tomcat App与数据库无连接
- php - How to add the product again to the cart or add to cart button not working?