为了账号安全,请及时绑定邮箱和手机立即绑定

简单 JavaScript 中的未定义输出

简单 JavaScript 中的未定义输出

手掌心 2022-12-22 15:35:27
我对这一切都很陌生,我最近开始学习 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()
查看完整描述

3 回答

?
蛊毒传说

TA贡献1895条经验 获得超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()


查看完整回答
反对 回复 2022-12-22
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

欢迎来到编程!上面的评论是正确的——当你发帖时,一定要真正具体地说明你所看到的问题。让您更容易获得帮助。

尽管如此,我认为通过查看上面的代码我可以明白你的意思。在调试时了解代码的运行方式通常很有帮助:

  1. playGame() 被称为

  2. 使用参数“rock”调用 getUserChoice

  3. userChoice 被分配为 'Rock' *注意大写

  4. determineWinner 以 'Rock' 作为 userChoice 调用

  5. 'Rock' 不会触发任何 if 语句,并且 determineWinner 不会返回任何内容

因此,通过执行这些步骤,实际上很容易看出为什么 determineWinner 在注销时未定义……它不返回任何内容。您的 getUserChoice 函数的要点似乎是标准化输入。但是那些标准化的输入后来没有被正确使用。您可以考虑将这些可能的值存储在一个数组中,然后从该函数返回小写值吗?

希望这有帮助,祝你好运!


查看完整回答
反对 回复 2022-12-22
?
月关宝盒

TA贡献1772条经验 获得超5个赞

您只在确定获胜者方法中检查 Rock 和 Sciccors


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 === '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()


查看完整回答
反对 回复 2022-12-22
  • 3 回答
  • 0 关注
  • 80 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信