Paper, Scissor, Rock!

Can someone tell me why my code isn’t working?! I’m so confused!!! It just puts [Function: playGame] at the end when I try to put console.log it.

const getUserChoice = userInput => { userInput = userInput.toLowerCase(); if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') { return userInput; } else { console.log('Error, please type: 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 'This game is a tie!'}; if(userChoice === 'rock'){ if (computerChoice === 'paper'){ return "Computer won!"; } else { return "You won!"; } } if(userChoice === 'paper'){ if (computerChoice === 'scissor') return "Computer won!" else { return 'You won!' } } if(userChoice === 'scissor'){ if (computerChoice === 'rock') return "Computer won!" else{ return 'You won!'; } } }; const playGame = () => { const userChoice = userChoice('paper'); const computerChoice = computerChoice(); console.log(`You threw: ${userChoice} `); console.log(`The computer threw: ${computerChoice}`); console.log(determineWinner(userChoice, computerChoice)); }; console.log(playGame);

Since you log the result of the playGame function inside its body rather than returning it, you don’t need to log the return value of the function call of playGame as there is none. But you do need to call the function playGame. In line 56 write playGame() rather than console.log(playGame);

Hi, few problems with your code:

  1. on line 56 when you want to call the function playGame(), just call it instead of console.log(playGame), which just logs the function into console.
  2. on line 48, userChoice(‘paper’) is not a declared function, you may use const userChoice = getUserChoice(), which is a function you created earlier.
  3. on line 49, same problem as above, use const computerChoice = getComputerChoice(), because this is the function that you created earlier.

I recently learned a tip that is really useful: look at the errors and try to understand the errors will benefit significantly for your learning.

Wish this helps :slight_smile:

Nice one. Keep up, but there are few errors in your code.

The forum post discusses an issue with the “Rock, Paper, Scissors” game code, where the function is not working as expected. Key errors include calling userChoice and computerChoice functions incorrectly, and logging the function itself instead of executing it. Users suggest fixing the function calls and correcting syntax errors to make the game run properly.

Please stop posting CGPT replies. They aren’t helpful and they violate Community Guidelines.

1 Like