'Undefined' output for Rock, Paper, Scissors project

I was doing the rockPaperScissors.js project and I finished every task and got the correct output. But for some reason after the userChoice, computerChoice, and determineWinner output there is an undefined.

You threw: scissors
The computer threw: paper
You win!
undefined

I do not know where this undefined is coming from.
Here is the code:

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors' || userInput === 'bomb') { return userInput; } else { console.log('ERROR! Make sure you entered the correct choice.'); } }; const getComputerChoice = () => { 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 ends in a tie.'; } if (userChoice === 'rock') { if (computerChoice === 'paper') { return 'You lost'; } else { return 'You won!'; } } ; if (userChoice === 'paper') { if (computerChoice === 'rock') { return 'You win!'; } else { return 'You lost'; } } if (userChoice === 'scissors') { if (computerChoice === 'paper') { return 'You win!'; } else { return 'You lost'; } } if (userChoice === 'bomb') { return 'You win!'; } }; const playGame = () => { const userChoice = getUserChoice('scissors'); const computerChoice = getComputerChoice(); console.log(`You threw: ${userChoice}`); console.log(`The computer threw: ${computerChoice}`); console.log(determineWinner(userChoice, computerChoice)); }; console.log(playGame());
1 Like

Your function playGame does not have a return value, so your console.log(playGame()); line returns undefined

To fix this, just change the last line to playGame();

1 Like