This function is supposed to produce exactly 'rock', 'paper', or 'scissors' (input is not case sensitive). But when I put in the wrong value (ie. 'papers' with an extra 's'), my function prints the error message (expected) AND a message saying "undefined" (not expected).
const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if (userInput === 'rock' ||
userInput === 'paper' ||
userInput === 'scissors') {
return userInput;
} else {
console.log('Please enter rock, paper, or scissors.')
}
}
console.log(getUserChoice('papers')); #prints 'paper' correctly if paper is the input
EDIT: I updated the question to be clearer and I already found the answer. It has to do with the difference between console.log and return.
It's because your console.loging the error message rather then returning it
and when it ask you to 'Please enter rock, paper, or scissors' your not returning anything and thus the return is undefined.
so switch
console.log('Please enter rock, paper, or scissors')
to
return 'Please enter rock, paper, or scissors';