Rock, Paper, Scissors : undefined error

Again, stuck with the step 13 of
https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/bapi-javascript-conditionals-and-functions/modules/learn-javascript-functions/projects/rock-paper-scissors-javascript .

Now it’s throwing the following error once I run the code:

You threw: paper
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:60
  console.log('The computer threw:' + computerChoice);
                                      ^

ReferenceError: computerChoice is not defined
    at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:60:39)
    at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:65:1)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

Looking at the past posts, I figured that my code is missing or having extra curly brace/s. But I appreciate your insights into why I am getting this error. The source code is here:

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();

  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors'){
    return userInput;
  } else {
    console.log('error');
  }
};


const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);

  if (randomNumber === 0){
    return 'rock';
  } else if (randomNumber ===  1){
    return 'paper';
  } else if (randomNumber ===  2){
    return 'scissors';
  }
};


const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice){
    return 'The game is a tie!';
  }

  if (userChoice === 'rock'){
    if (computerChoice == 'paper'){
      return 'the computer won!';
    } else {
      return 'you won';
    }
  }

  if(userChoice === 'paper'){
    if(computerChoice === 'scissors'){
      return 'computer won';
    } else {
      return 'you won';
    }
  }

  if(userChoice === 'scissors'){
    if(computerChoice === 'paper'){
      return 'computer won';
    } else {
      return 'you won';
    }
  }
};


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

playGame();

When you see the error variable is not defined, the first thing you should check is the line on which you actually define the variable, and see if there could be any reason causing that. Check the definition of computerChoice and see if you spot any problems with it.

1 Like

Hey awkwineh183, my mate!
Following Adam suggestion you’ll see that the error points to this function:

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

Now, if you look closely you got a typo here:
const computeChoice = getComputerChoice();
instead of:
const computerChoice = getComputerChoice();

if you correct it, it should work now:

Thank you so much, I just corrected that typo. It’s perfectly working now :slight_smile:

1 Like