所以我一直在尝试学习编码,并一直使用codeAcademy作为资源。我目前正在做一个石头,剪刀,纸的练习,我的代码运行正常,但是我的上一个console.log(determineWinner(userChoice, computerChoice));"
向控制台扔了一个undefined
。任何帮助将不胜感激。
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
return userInput;
} else {
console.log('Error!');
}
}
const getComputerChoice = () => {
switch (Math.floor(Math.random() * 3)) {
case 0:
return 'rock';
case 1:
return 'paper';
case 2:
return 'scissors';
}
}
const determineWinner = (userChoice, computerChoice) => {
if (getUserChoice === getComputerChoice) {
return 'the game was a tie';
}
}
if (getUserChoice === 'rock') {
if (getComputerChoice === 'paper') {
return 'computer won!';
} else {
return 'you won!';
}
}
if (getUserChoice === 'paper') {
if (getComputerChoice === 'scissors') {
return 'computer won!';
} else {
return 'you won!';
}
}
if (getUserChoice === 'scissors') {
if (getComputerChoice === 'rock') {
return 'computer won!';
} else {
return 'you won!';
}
}
const playGame = () => {
const userChoice = getUserChoice('rock');
const computerChoice = getComputerChoice();
console.log(`You threw: ${userChoice}`);
console.log(`The computer threw: ${computerChoice}`);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
答案 0 :(得分:2)
忽略您在此处发布的代码中的一个小语法错误,无效代码的问题是在函数determineWinner
中,您有两个分别名为userChoice
和{{1}的变量}。但是,错误地,您正在使用computerChoice
和getUserChoice
。
getComputerChoice