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

标签 javascript if-statement

刚开始学习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/

相关文章:

javascript - Angular 不绑定(bind)在 img 标签内

python - 在 if 中定义变量

string - Bash shell 中的 "[ ]"与 "[[ ]]"

javascript - 删除隐藏的表格行并将 .html 输出到 Excel

javascript - 如何禁用在 v-time-picker 小部件中选择分钟?

ios - UITextview If条件并添加 `+`符号

python - python忽略if语句

javascript - 异步使用 javascript Promise

javascript - 在最后一页提交之前,我们如何跨不同页面存储多个字段值。

javascript - 如何使用 javascript 和 imacros 读取文件名