javascript - 无法正确执行 else if 语句
问题描述
我正在创建一个带有函数和 if 语句的 Scissors Paper 摇滚游戏。我目前遇到一个错误,它只返回 DRAW 案例。如果其他人能发现这个错误,将不胜感激。
我的代码的链接在这里: https ://github.com/BladeMountain/SissorPaperRock/blob/master/SPR_script.html
function computerPlay() {
let choice = ['rock', 'paper', 'scissor'];
let randomChoice = [Math.floor(Math.random() * choice.length)];
return randomChoice;
}
function playGame(playerSelection, computerSelection) {
if (playerSelection == "scissor" && computerSelection == "paper") {
return "You Won";
} else if (playerSelection == "paper" && computerSelection == "rock") {
return "You Won";
} else if (playerSelection == "rock" && computerSelection == "scissor") {
return "You Won";
} else if (playerSelection == "scissor" && computerSelection == "rock") {
return "You Lost";
} else if (playerSelection == "paper" && computerSelection == "scissor") {
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "paper") {
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "rock") {
return "Draw";
} else if (playerSelection == "scissor" && computerSelection == "scissor") {
return "Draw";
} else if (playerSelection == "paper" && computerSelection == "paper") {
return "Draw";
} else {
return 'Invalid response, this is case insensitive. Please input either scissor, paper or rock';
}
}
function game() {
let roundOne = playGame(playerSelection, computerSelection);
let roundTwo = playGame(playerSelection, computerSelection);
let roundThree = playGame(playerSelection, computerSelection);
let roundFour = playGame(playerSelection, computerSelection);
let roundFive = playGame(playerSelection, computerSelection);
console.log('Round 1 = ' + roundOne);
console.log('Round 2 = ' + roundTwo);
console.log('Round 3 = ' + roundThree);
console.log('Round 4 = ' + roundFour);
console.log('Round 5 = ' + roundFive);
}
let playerSelection = prompt("Please select one", "scissor/paper/rock");
let computerSelection = computerPlay();
console.log(playGame(playerSelection, computerSelection));
// this is only still executing my else statement. Anything that I need to change?
另外,我是 StackOverflow 和编码的新手,希望这不是一个愚蠢的问题。
解决方案
请检查下面的代码....我在函数 computerPlay() 中进行了更改
而不是返回随机数返回数组值...返回函数 computerPlay() 只是一个变化
// this is only still executing my else statement. Anything that I need to change?
function computerPlay() {
let choice = ['rock', 'paper', 'scissor'];
let randomChoice = [Math.floor(Math.random()*choice.length)];
return choice[randomChoice];
}
function playGame(playerSelection, computerSelection) {
if (playerSelection == "scissor" && computerSelection == "paper"){
return "You Won";
} else if (playerSelection == "paper" && computerSelection == "rock"){
return "You Won";
} else if (playerSelection == "rock" && computerSelection == "scissor"){
return "You Won";
} else if (playerSelection == "scissor" && computerSelection == "rock"){
return "You Lost";
} else if (playerSelection == "paper" && computerSelection == "scissor"){
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "paper"){
return "You Lost";
} else if (playerSelection == "rock" && computerSelection == "rock"){
return "Draw";
} else if (playerSelection == "scissor" && computerSelection == "scissor"){
return "Draw";
} else if (playerSelection == "paper" && computerSelection == "paper"){
return "Draw";
} else {
return'Invalid response, this is case insensitive. Please input either scissor, paper or rock';
}
}
function game() {
let roundOne = playGame(playerSelection, computerSelection);
let roundTwo = playGame(playerSelection, computerSelection);
let roundThree = playGame(playerSelection, computerSelection);
let roundFour = playGame(playerSelection, computerSelection);
let roundFive = playGame(playerSelection, computerSelection);
console.log('Round 1 = ' + roundOne);
console.log('Round 2 = ' + roundTwo);
console.log('Round 3 = ' + roundThree);
console.log('Round 4 = ' + roundFour);
console.log('Round 5 = ' + roundFive);
}
let playerSelection = prompt("Please select one", "scissor/paper/rock");
let computerSelection = computerPlay();
console.log(playGame(playerSelection, computerSelection));
// this is only still executing my else statement. Anything that I need to change?
<!DOC HTMl>
<html>
</html>
推荐阅读
- elasticsearch - 基于术语聚合结果的弹性搜索过滤器
- robotics - 如何将本地子程序名称存储在数组中并在循环中调用它们,并在 KUKA 机器人语言中遍历所述数组
- python - Pipenv 中的 Python 脚本作为具有 NSSM 的 Windows 服务
- php - 用 php 编辑 csv
- android - 做部分代码缓存收集,code=24KB,data=30KB android studio 3.3.2
- c++ - 为什么 C++20 中的 std::move 不是 [[nodiscard]]?
- c# - UWP 存储库的名称是什么?
- r - readline 不会提示用户从 Rstudio 中的 Rprofile.site 输入
- c# - 如何将父节点添加到 xml 文件?
- c# - 检查2个datagridview中是否有重复值