繁体   English   中英

为什么我的函数总是返回相同的值?

[英]Why does my function always return the same value?

最近在学习 Javascript,所以我正在学习在对象中存储函数。 目前正在创建一个简单的石头、纸、剪刀游戏。 我被卡住了,我需要 getWinner 函数来确定获胜者,并向函数添加了 3 个条件 = 平局、赢或输。 现在的问题是,它总是返回给我。 任何人都可以帮忙吗?

 const startGameBtn = document.getElementById('start-game-btn'); let ROCK = "ROCK"; let PAPER = "PAPER"; let SCISSORS = "SCISSORS"; let RESULT_DRAW = "It's a draw"; let RESULT_PLAYER_WINS = "Player Wins"; let RESULT_COMPUTER_WINS = "Player Wins"; let GAME_IS_RUNNING = false; let getPlayerChoice = function () { let selection = prompt(`${ROCK},${PAPER}, or ${SCISSORS}? `, '').toUpperCase(); if (selection !== ROCK && selection !== PAPER && selection !== SCISSORS) { alert ("Invalid choice, defaulted to Rock"); selection = ROCK; } return selection } const getComputerChoice = function() { const randomValue = Math.floor(Math.random() * 2); if (randomValue === 0) { return ROCK; } else if (randomValue === 1) { return PAPER; } else if (randomValue === 2) { return SCISSORS; }; } const getWinner = function (cChoice, pChoice) { if (cChoice === pChoice) { return RESULT_DRAW; } else if (cChoice === ROCK && pChoice === PAPER || cChoice === PAPER && pChoice === SCISSORS || cChoice === SCISSORS && pChoice === ROCK ) { return RESULT_PLAYER_WINS; } else { return RESULT_COMPUTER_WINS; } } startGameBtn.addEventListener('click', function () { if (GAME_IS_RUNNING) { return } GAME_IS_RUNNING = true; console.log("Game is starting...."); let playerChoice = getPlayerChoice(); console.log(playerChoice); let computerChoice = getComputerChoice(); console.log(computerChoice); let winner = getWinner(computerChoice, playerChoice); console.log(winner); });
 <button id="start-game-btn">Start</button>

只修复电脑永远无法选择剪刀的问题,它运行正常。 也许你只是幸运地抽奖了。

 const startGameBtn = document.getElementById('start-game-btn'); let ROCK = "ROCK"; let PAPER = "PAPER"; let SCISSORS = "SCISSORS"; let RESULT_DRAW = "It's a draw"; let RESULT_PLAYER_WINS = "Player Wins"; let RESULT_COMPUTER_WINS = "Player Wins"; let GAME_IS_RUNNING = false; let getPlayerChoice = function() { let selection = prompt(`${ROCK},${PAPER}, or ${SCISSORS}? `, '').toUpperCase(); if (selection !== ROCK && selection !== PAPER && selection !== SCISSORS) { alert("Invalid choice, defaulted to Rock"); selection = ROCK; } return selection } const getComputerChoice = function() { const randomValue = Math.floor(Math.random() * 3); // <---- if (randomValue === 0) { return ROCK; } else if (randomValue === 1) { return PAPER; } else if (randomValue === 2) { return SCISSORS; }; } const getWinner = function(cChoice, pChoice) { if (cChoice === pChoice) { return RESULT_DRAW; } else if (cChoice === ROCK && pChoice === PAPER || cChoice === PAPER && pChoice === SCISSORS || cChoice === SCISSORS && pChoice === ROCK ) { return RESULT_PLAYER_WINS; } else { return RESULT_COMPUTER_WINS; } } startGameBtn.addEventListener('click', function() { if (GAME_IS_RUNNING) { return } GAME_IS_RUNNING = true; console.log("Game is starting...."); let playerChoice = getPlayerChoice(); console.log(playerChoice); let computerChoice = getComputerChoice(); console.log(computerChoice); let winner = getWinner(computerChoice, playerChoice); console.log(winner); });
 <button id="start-game-btn">START-STOP</button>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM