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

TA貢獻1995條經驗 獲得超2個贊
歡迎來到編程!上面的評論是正確的——當你發帖時,一定要真正具體地說明你所看到的問題。讓您更容易獲得幫助。
盡管如此,我認為通過查看上面的代碼我可以明白你的意思。在調試時了解代碼的運行方式通常很有幫助:
playGame() 被稱為
使用參數“rock”調用 getUserChoice
userChoice 被分配為 'Rock' *注意大寫
determineWinner 以 'Rock' 作為 userChoice 調用
'Rock' 不會觸發任何 if 語句,并且 determineWinner 不會返回任何內容
因此,通過執行這些步驟,實際上很容易看出為什么 determineWinner 在注銷時未定義……它不返回任何內容。您的 getUserChoice 函數的要點似乎是標準化輸入。但是那些標準化的輸入后來沒有被正確使用。您可以考慮將這些可能的值存儲在一個數組中,然后從該函數返回小寫值嗎?
希望這有幫助,祝你好運!

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()
添加回答
舉報