JavaScript 抛出未定义的错误

标签 javascript

所以我一直在尝试学习编码并一直使用 codeAcademy 作为资源。我目前正在做石头、剪刀、布练习,我的代码运行良好,但我的最后一个 console.log(defineWinner(userChoice, ComputerChoice));" 抛出 undefined进入控制台。任何帮助将不胜感激。

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

const getComputerChoice = () => {
  switch (Math.floor(Math.random() * 3)) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if (getUserChoice === getComputerChoice) {
    return 'the game was a tie';
  }
}
if (getUserChoice === 'rock') {
  if (getComputerChoice === 'paper') {
    return 'computer won!';
  } else {
    return 'you won!';
  }
}
if (getUserChoice === 'paper') {
  if (getComputerChoice === 'scissors') {
    return 'computer won!';
  } else {
    return 'you won!';
  }
}
if (getUserChoice === 'scissors') {
  if (getComputerChoice === 'rock') {
    return 'computer won!';
  } else {
    return 'you won!';
  }
}
const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log(`You threw: ${userChoice}`);
  console.log(`The computer threw: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
};
playGame();

最佳答案

忽略您在此处发布的代码中的一个小语法错误,非工作代码的问题是在函数 defineWinner 中,您有两个名为 userChoice 的变量> 和computerChoice。但是,您错误地使用了 getUserChoicegetComputerChoice

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
    return userInput;
  } else {
    console.log('Error!');
  }
}

const getComputerChoice = () => {
  switch (Math.floor(Math.random() * 3)) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'the game was a tie';
  }

  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'computer won!';
    } else {
      return 'you won!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
      return 'computer won!';
    } else {
      return 'you won!';
    }
  }
  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'computer won!';
    } else {
      return 'you won!';
    }
  }
}
const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();
  console.log(`You threw: ${userChoice}`);
  console.log(`The computer threw: ${computerChoice}`);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

关于JavaScript 抛出未定义的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51340118/

相关文章:

javascript - 带返回值的跨域弹窗

javascript - Vue.js - 具有动态组件的多个事件

javascript - 刷新页面后 Gif 图像没有动画

javascript - 如何在 ThreeJS 中为每个点赋予自己的颜色

javascript - 具有 CSS 过渡效果的 jquery 悬停功能仅适用于第二次悬停

javascript - 在 ionic 3 上的 ion-nav 上的 StatusBar

javascript - 我是 es6 javascript 新手

用于自动完成的 Javascript 数组

javascript - 如何在 RESTful 系统中使用 HTTP GET 请求传递授权数据?

javascript - 在此示例中,多重选择是如何工作的?