我对这一切都很陌生,我最近开始学习 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()
蛊毒传说
拉风的咖菲猫
月关宝盒
相关分类