亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

簡單 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 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號