Rock, Paper, or Scissors function problem

https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

On Rock, Paper, Scissors task, exercise 5,

Create a new function named getComputerChoice with no parameters. Inside its block, utilize Math.random() and Math.floor() to get a whole number between 0 and 2. Then, depending on the number, return either 'rock' , 'paper' , or 'scissors' .

so I wrote this code:

function getComputerChoice() {
  const randomNumber = Math.floor(Math.random()*3);
  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return : 'scisscors';
  };
}

I got error. The project walkthrough shows that he used function expression not this “function declaration” but I don’t understand why this is wrong?

I don’t think this is the problem, why would this be relevant?

I think the problem is here:

return : 'scisscors';

seems you made a typo/grammar mistake.

Thanks a lot!! now it is all good :))

I am also stuck on this project. I am getting

When I run the code, I get this in the console

scissors
You threw: Undefined
the Computer threw: paper
Undefined

Do you know why this is happening and also how I should go about troubleshooting it?

This is my code:

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {console.log(userInput);
  }  else {console.log('invalid user input')}
}

function getComputerChoice() {
  const randomNumber = Math.floor(Math.random() * 3);
switch (randomNumber) {
  case 0:
  return 'rock';
  case 1:
  return 'paper';
  case 2:
  return 'scissors';
}
}
function determineWinner(getUserChoice, getComputerChoice) {
  if(getUserChoice === getComputerChoice){
    return 'The game is a tie!'
  } if (getUserChoice === 'rock') {
    if(getComputerChoice ==='paper') {
    return 'The computer won!';
  } else {
    return 'You won!';
  }
  }
  if(getUserChoice === 'paper') {
    if(getComputerChoice === 'scissors'){
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  }
  if(getUserChoice === 'scissors'){
    if(getComputerChoice === 'rock'){
      return 'The computer won!'
    } else {
      return 'You won!';
    }
  }
}

function playGame() {
 const userChoice = getUserChoice('scissors');
 const computerChoice = getComputerChoice();
 console.log('You threw: '+ userChoice)
 console.log('The computer threw: ' + computerChoice);
 console.log(determineWinner(userChoice, computerChoice))
}
playGame()

here:

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if(userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {console.log(userInput);
  }  else {console.log('invalid user input')}
}

getUserChoice doesn’t return the userInput.

Amazing, thanks so much for your speedy reply! Got it working now :slight_smile:

I don’t understand, you have output:

You threw: Undefined

which exactly tells you about this problem.

I’m just inexperienced

yeah, this exercise is quite difficult for beginners (like me). Keep learning! :smile:

1 Like

@nicolahearn I think what stetim94 was trying to get at is that sometimes we can find what’s wrong with our code just by examining the behavior of the program and the outputs we are getting back.

Especially as beginners, we shouldn’t be shy to add console.log() whenever we feel like we need to check that variables are holding the values we expected. Basically, getting good at debugging is super useful!

Yeah I just didn’t know that ‘undefined’ meant I hadn’t added a line to return the word. But now I do :grin:

Actually, if you look at documentation (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures)
you’ll realize that undefined is a data type.

Based on this: https://developer.mozilla.org/en-US/docs/Glossary/Undefined

undefined is a primitive value automatically assigned to variables that have just been declared, or to formal arguments for which there are no actual arguments.

You were getting undefined because your function wasn’t returning anything, so JS was giving it the value of undefined! Awesome, right?

1 Like