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()
添加回答
举报