简单 JavaScript 中的未定义输出

我对这一切都很陌生,我最近开始学习 JavaScript。为了测试我的学习情况,我制作了这个简单的脚本、石头剪刀布。它与 Codecademy 项目非常相似。我遇到的问题是输出,输出为“未定义”,我无法弄清楚,这个输出是什么,有人可以帮忙吗?


const getUserChoice = userInput => {

  userInput = userInput.toLowerCase();


  if (userInput === 'rock') {

    return 'Rock' 

  } else if (userInput === 'paper') {

    return 'Paper' }

    else if (userInput === 'scissors') {

    return 'Scissors'} 

    else if (userInput === 'bomb') {

      return 'Bomb'

    } else {

        return 'Please input a valid choice!'

      }

      }


const getComputerChoice = () => {

  const numbers = (Math.floor(Math.random() * 3))


  switch (numbers) {

    case 0 : return "Rock";

    break;

    case 1 : return "Paper";

    break;

    case 2 : return "Scissors";

    break;

  } 

}


const determineWinner = (userChoice, computerChoice) => {

  if (userChoice === computerChoice) {

      return 'It\'s a tie!!';

    } 

  if (userChoice === 'rock') {

    if (computerChoice === 'paper') {

       return 'The Computer has won the game!!';

    } else {

        return 'Congratulation You have won the game!!';

    }

  }

  if (userChoice === 'scissors') {

    if (computerChoice === 'rock') {

      return ('The Computer has won the game!!');

    } else {

       return ('Congratulations You have won the game!!');

    }

  }

  if (userChoice === 'scissors') {

    if (computerChoice === 'paper') {

      return 'Cogratulations You have Won the game!!';

  } else {

      return 'The Computer has won the game!!';

  }

}

  if (userChoice === 'bomb') {

    return 'Congratulation you Won!!'

  }


};


const playGame = () => {

  var userChoice =  getUserChoice('rock')

  var computerChoice = getComputerChoice()

 console.log('You picked: ' + userChoice);

 console.log('The computer picked: ' +computerChoice)


  console.log(determineWinner(userChoice, computerChoice));

}

 playGame()


手掌心
浏览 103回答 3
3回答

蛊毒传说

你的userChoice和computerChoice都是大写的。您正在根据小写字符串检查它们。此外,您正在检查剪刀两次而不是检查纸张。const getUserChoice = userInput => {  userInput = userInput.toLowerCase();  if (userInput === 'rock') {    return 'Rock'  } else if (userInput === 'paper') {    return 'Paper'  } else if (userInput === 'scissors') {    return 'Scissors'  } else if (userInput === 'bomb') {    return 'Bomb'  } else {    return 'Please input a valid choice!'  }}const getComputerChoice = () => {  const numbers = (Math.floor(Math.random() * 3))  switch (numbers) {    case 0:      return "Rock";      break;    case 1:      return "Paper";      break;    case 2:      return "Scissors";      break;  }}const determineWinner = (userChoice, computerChoice) => {  if (userChoice === computerChoice) {    return 'It\'s a tie!!';  }  if (userChoice === 'Rock') {    if (computerChoice === 'Paper') {      return 'The Computer has won the game!!';    } else {      return 'Congratulation You have won the game!!';    }  }  if (userChoice === 'Paper') {    if (computerChoice === 'Rock') {      return ('The Computer has won the game!!');    } else {      return ('Congratulations You have won the game!!');    }  }  if (userChoice === 'Scissors') {    if (computerChoice === 'Paper') {      return 'Cogratulations You have Won the game!!';    } else {      return 'The Computer has won the game!!';    }  }  if (userChoice === 'Bomb') {    return 'Congratulation you Won!!'  }};const playGame = () => {  var userChoice = getUserChoice('rock')  var computerChoice = getComputerChoice()  console.log('You picked: ' + userChoice);  console.log('The computer picked: ' + computerChoice)  console.log(determineWinner(userChoice, computerChoice));}playGame()

拉风的咖菲猫

欢迎来到编程!上面的评论是正确的——当你发帖时,一定要真正具体地说明你所看到的问题。让您更容易获得帮助。尽管如此,我认为通过查看上面的代码我可以明白你的意思。在调试时了解代码的运行方式通常很有帮助:playGame() 被称为使用参数“rock”调用 getUserChoiceuserChoice 被分配为 'Rock' *注意大写determineWinner 以 'Rock' 作为 userChoice 调用'Rock' 不会触发任何 if 语句,并且 determineWinner 不会返回任何内容因此,通过执行这些步骤,实际上很容易看出为什么 determineWinner 在注销时未定义……它不返回任何内容。您的 getUserChoice 函数的要点似乎是标准化输入。但是那些标准化的输入后来没有被正确使用。您可以考虑将这些可能的值存储在一个数组中,然后从该函数返回小写值吗?希望这有帮助,祝你好运!

月关宝盒

您只在确定获胜者方法中检查 Rock 和 Sciccorsconst getUserChoice = userInput => {  userInput = userInput.toLowerCase();  if (userInput === 'rock') {    return 'Rock'   } else if (userInput === 'paper') {    return 'Paper' }    else if (userInput === 'scissors') {    return 'Scissors'}     else if (userInput === 'bomb') {      return 'Bomb'    } else {        return 'Please input a valid choice!'      }      }const getComputerChoice = () => {  const numbers = (Math.floor(Math.random() * 3))  switch (numbers) {    case 0 : return "Rock";    break;    case 1 : return "Paper";    break;    case 2 : return "Scissors";    break;  } }const determineWinner = (userChoice, computerChoice) => {  if (userChoice === computerChoice) {      return 'It\'s a tie!!';    }   if (userChoice === 'Rock') {    if (computerChoice === 'Paper') {       return 'The Computer has won the game!!';    } else {        return 'Congratulation You have won the game!!';    }  }  if (userChoice === 'Scissors') {    if (computerChoice === 'Rock') {      return ('The Computer has won the game!!');    } else {       return ('Congratulations You have won the game!!');    }  }  if (userChoice === 'Paper') {//You mean paper here    if (computerChoice === 'Rock') {      return 'Cogratulations You have Won the game!!';  } else {      return 'The Computer has won the game!!';  }}  if (userChoice === 'bomb') {    return 'Congratulation you Won!!'  }};const playGame = () => {  var userChoice =  getUserChoice('rock')  var computerChoice = getComputerChoice() console.log('You picked: ' + userChoice); console.log('The computer picked: ' +computerChoice)  console.log(determineWinner(userChoice, computerChoice));} playGame()
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript