Rock, Scissors, Paper with Undefined Result

Hi all, I get stuck in this project.
When I tried to console.log with different userChoice(s) and computerChoice(s), there wasn’t an issue. However, when I try to have it printed under the playGame() function, the result became ‘undefined’. Could anyone shade some light please. Thank you.

My code is below:

const getUserChoice = (userInput) => {

userInput = userInput.toLowerCase();

if(userInput === ‘scissors’|| userInput === ‘rock’|| userInput ===‘paper’){

return `Your choice is ${userInput}.`

} else{

console.log('Please enter either rock, paper, or scissors.')

}

}

const getComputerChoice = () =>{

const randomNumber = Math.floor(Math.random () *3);

switch(randomNumber){

case 0:

return 'rock';



case 1:

return 'paper';



case 2:

return 'scissors';

}

}

const determineWinner = (userChoice, computerChoice) => {

if (userChoice === computerChoice){

return 'The game was a tie';}

if(userChoice === ‘rock’){

if(computerChoice === ‘paper’){

return 'Sorry, the computer won!';

} else{

return 'Yay! You won!';

}

}

if(userChoice === ‘paper’){

if (computerChoice === ‘scissors’){

return ‘Sorry, the computer won!’;

} else{

return 'Yay! You won!';

}

}

if(userChoice === ‘scissors’){

if (computerChoice === ‘rock’){

return ‘Sorry, the computer won!’;

} else{

return 'Yay! You won!';

}

}

}

/*

console.log(determineWinner(‘scissors’,‘rock’));

console.log(determineWinner(‘paper’,‘rock’));

console.log(determineWinner(‘rock’,‘scissors’));

*/

const playGame = () => {

let userChoice = getUserChoice(‘scissors’);

let computerChoice = getComputerChoice();

console.log(‘You threw:’ + userChoice);

console.log(‘The computer threw:’ + computerChoice);

console.log(determineWinner(userChoice,computerChoice));

};

playGame();

1 Like

Hello! Have a look at the getUserChoice function. It returns

But in determineWinner, you are only comparing single words (scissors vs rock vs paper).

3 Likes

Aha, fixed! Thanks a million! :star_struck:

1 Like