I have been really struggling with this exercise. Now I have managed to get the code to spit out the choices of myself and the computer, but often I receive an undefined, when I want to call the winner.
Example:
I throw rock
Comp throws paper
undefined
So here must be a problem with the else statement.
I also always get undefined when I try to throw bomb. So there must be a problem in the determineWinner function, but it looks okay to me.
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === 'rock') {
return userInput
} if (userInput === 'paper') {
return userInput
} if (userInput === 'scissors') {
return userInput
} if (userInput === 'bomb') {
return userInput
} else {
console.log ('Wrong user input.');
}
} ;
//console.log(getUserChoice('banana'));
const getComputerChoice = () => {
const randomNumber = Math.floor(Math.random() * 3);
if (randomNumber === 0) {
return 'rock';
} if (randomNumber === 1) {
return 'paper';
} if (randomNumber === 2) {
return 'scissors';
}
};
//console.log(getComputerChoice());
const determineWinner = (userChoice, computerChoice) => {
if (userChoice === computerChoice) {
return 'This is a tie.'
if (userChoice === 'rock') {
if (computerChoice === 'paper') {
return 'The computer won!'
} else {
return 'You lost'
}
} if (userChoice === 'paper') {
if (computerChoice ==='scissors') {
return 'The computer wins!'
} else {
return 'You won!'
}
} if (userChoice === 'scissors') {
if (computerChoice === 'paper') {
return 'You win!'
} else {
return 'The computer wins!'
}
} if (userChoice === 'bomb') {
return 'You win big time!'
}
}
};
const playGame = () => {
const userChoice = getUserChoice('bomb');
const computerChoice = getComputerChoice();
console.log('You threw ' + userChoice);
console.log('The computer threw: ' + computerChoice);
console.log(determineWinner(userChoice, computerChoice));
};
playGame();
Could someone please have a look and tell me what I am doing wrong? Thank you.
Thank you @codeneutrino. That was the problem. Any tips on how to prevent this in the future? I feel like I am blind to those small details, once I have several code blocks.
Honestly, not really. If you find that there’s weird behaviour, it is often something like this. In most IDES/Text Editors (of which the CC learning environment is one), if you click on an opening/closing bracket, it will highlight the corresponding closing/opening bracket.
Hey guys I think I’m having a similar problem on the previous step for this project. I did find one missing bracket after reading this thread and that solved one part but I’m still getting ‘undefined’ fir user choice ‘paper’. Where ‘userChoice === computerChoice’ no problem
const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) {
return ‘This game is a tie!’;
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘computer won!’;
}else {
return ‘You won son!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘computer won,!’;
}else{
return ‘You won son!’;
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘computer won!’
}else{
return ‘You won son!’
}}}}}
Hi Everyone,
I’m also struggling with this one. I think I’ve got all the code, but I keep getting the response that ‘userChoice’ is undefined. I’ve tried following the walkthrough, but no luck.
Hello @jenzi123 , welcome to the forums! Could you say on which line this error originates, please? Also, could you format your code according to these guidelines?
Hey guys I think I’m having a similar problem on the previous step for this project. I did find one missing bracket after reading this thread and that solved one part but I’m still getting ‘undefined’ fir user choice ‘paper’. Where ‘userChoice === computerChoice’ no problem
const determineWinner = (userChoice, computerChoice) => { if (userChoice === computerChoice) {
return ‘This game is a tie!’;
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘computer won!’;
}else {
return ‘You won son!’;
}
if (userChoice === ‘rock’) {
if (computerChoice === ‘paper’) {
return ‘computer won,!’;
}else{
return ‘You won son!’;
}
if (userChoice === ‘paper’) {
if (computerChoice === ‘scissors’) {
return ‘computer won!’
}else{
return ‘You won son!’
}}}}}
console.log(determineWinner(‘scissors’, ‘paper’))
console.log(determineWinner(‘scissors’, ‘rock’))
console.log(determineWinner(‘scissors’, ‘scissors’))
console.log(determineWinner(‘paper’, ‘rock’))
console.log(determineWinner(‘paper’, ‘scissors’))
console.log(determineWinner(‘paper’, ‘paper’))
//it prints to the console
You won son!
computer won!
This game is a tie!
undefined
undefined
This game is a tie!