Rock, Paper, Scissors Help

I am stuck on the Rock, Paper, Scissors exercise.
https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

I keep receiving undefined in the console and I have no idea why.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('Error. Please pick either rock, paper, or scissors');
  }
};
function getComputerChoice () {
  switch(Math.floor(Math.random()*2)) {
    case 0:
    return 'rock';
    break;
    case 1:
    return 'paper';
    break;
    case 2:
    return 'scissors';
    break;
  }
};
function determineWinner (userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'The game was a tie!';
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'The computer won.';
    }
    else {
      return 'You win!';
    }
  }
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors'){
      return 'The computer won.';
    }
    else {
      return 'You win!';
    }
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'paper'){
      return 'The computer won.';
    }
    else {
      return 'You win!';
    }
  }
};

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

Hello @microslayer54197, welcome to the forums! The problem is here:

Specifically, the 'rock'. You already have a condition were userChoice is “rock”. Because you have it again, and you are calling the function with “scissors”, which you don’t have an if statement for, that function return nothing.
I hope this helps!

I changed my code and now it works. Thanks!

1 Like