Computer threw:undefined


#1

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

I'm getting these errors and have no idea how to fix them:

  1. When I type something different than rock, paper, scissors I expect to see just the "Error!" message, instead I see this:
    Error!
    You threw: undefined
    Computer threw: undefined
    The game is a tie!

  2. When I type either rock, paper or scissors I get the following:
    You threw: paper
    Computer threw: undefined
    Computer won!

Any ideas, please?


function getUserChoice() {
  var userInput = prompt('Rock, Paper, or Scissors?');
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  }
  else {
    console.log('Error!');
  }
}

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

function determineWinner(userChoice,computerChoice) {
  if(userChoice === computerChoice) {
    return 'The game is a tie!';
  }
  
  if(userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer won!';
    } 
    else {
      return 'You won!';
    } 
  }
  
  if(userChoice === 'scissors') {
    if(computerChoice === 'paper') {
      return 'You won!';
    }
    else {
      return 'Computer won!';
    }
  }

  if(userChoice === 'paper') {
    if(computerChoice === 'rock') {
      return 'You won!';
    }
    else {
      return 'Computer won!';
    }
  }
}

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

playGame();


#2
  1. after error is logged, the rest of your function are still called:

var computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('Computer threw: ' + computerChoice);
  
  console.log(determineWinner(userChoice, computerChoice));

the game continues despite the error message.

  1. your function shouldn't have a parameter:

function getComputerChoice() {

store the result of the randomly generated number in randomNumber, then this problem should be solved.

as for the first problem, what do you want to do about it? what would be the desired behavior?


#3

Thanks a lot, I made it! I used this code:
var randomNumber = Math.floor(Math.random() * 3);


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.