[Rock, Paper, Scissors Exercise] I got the right result until I try to log the playGame function. How can I fix this problem?

Hi everyone!

Ive been working through the Rock, Paper, Scissors exercise (https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript) but Im getting an undefined error right after I try to log the playGame function (task 13).

Here is my code:

const getUserChoise = userInput =>{
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper' ){
  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 "Sorry, computer won!.";
    }else{
      return "Congratulations, you won!";
    }
  } 
  if (userChoice === 'paper') {
    if (computerChoice === 'scissors'){
      return "Sorry, computer won!";
    }else{
      return'Congratulations, you won!';
    }
  }
  if(userChoice === 'scissors'){
    if(computerChoice === 'rock'){
      return "Sorry, computer won!";
    }else{
      return "Congratulations, you won!";
    }
  }
};


const 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();

When I called the playGame function on the last line of my program, I got the message below:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:51
   const userChoice = getUserChoice('scissors');
                      ^

ReferenceError: getUserChoice is not defined
    at playGame (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:51:23)
    at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:59: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)

Thanks!

1 Like

How does this cause the following error?

ReferenceError: getUserChoice is not defined

Thanks for replying to the question! I`m still figuring out…

Choise vs Choice thats causing the ReferenceError.

OH! I could solve the problem!!! Thanks a lot :call_me_hand:

1 Like

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