gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:57:48 26 4
gpt4 key购买 nike

刚开始学习Javascript,所以我正在学习将函数存储在对象中。目前正在创建一个简单的剪刀石头布游戏。我卡住了,我需要 getWinner 函数来确定赢家,并向函数添加了 3 个条件 = 平局、赢或输。现在的问题是,它总是向我返回 draw。谁能帮忙?

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>

最佳答案

只修复了电脑永远不能选择SCISSORS的问题,运行正常。也许您只是中了幸运的一连串抽奖。

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>

关于javascript - 为什么我的函数总是返回相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59307871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com