rockPaperScissors 'Reference Error'

Hi, everyone,
i ve tried to figure out the issue now for 2 days and couldn’t solve it.
Please, can someone help me, to show and explain where i 've to put the reference (object) userInput and does my code will work after that, thanks in advance.
My code is down below and the error code.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper') {
    return userInput;
  } else {
    return console.log('Error:' + userInput);
  }
};

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

  switch (randomNumber) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'scissors';
      break;
    case 2:
      return 'paper';
      break;
  }
};

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

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

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

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

const playGame = () => {
  const userChoice = getUserChoice('rock');
  const computerChoice = getComputerChoice();

  console.log('You threw:' + userChoice);
  console.log('The computer threw:' + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:34
if (userChoice === 'rock') {
    ^

ReferenceError: userChoice is not defined
    at Object.<anonymous> (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:34:5)
    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)
    at bootstrap_node.js:542:3

The three if statements below the function determineWinner are out of scope. They should be inside the function determineWinner.

That is why you are getting ReferenceError

1 Like

In determineWinner function you should close the function block after your if statements, I think it’s just a typo. so fix that and move the close curly braces to the end of the function.

Also, you forget to pass arguments to the functions’s calls getUserChoice() and getComputerChoice() in determineWinner function . so fix it to:

  userChoice = getUserChoice(userChoice);
  computerChoice = getComputerChoice(computerChoice);

so your code will end up with something like that,

const getUserChoice = (userInput) => {
  userInput = userInput.toLowerCase();
  if (
    userInput === "rock" ||
    userInput === "scissors" ||
    userInput === "paper"
  ) {
    return userInput;
  } else {
    console.log("Error:" + userInput);
  }
};

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

  switch (randomNumber) {
    case 0:
      return "rock";
    case 1:
      return "scissors";
    case 2:
      return "paper";
  }
};

const determineWinner = (userChoice, computerChoice) => {
  userChoice = getUserChoice(userChoice);
  computerChoice = getComputerChoice(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 === "paper") {
    if (computerChoice === "scissors") {
      return "Computer won!";
    } else {
      return "You won!";
    }
  }

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

const playGame = () => {
  const userChoice = getUserChoice("rock");
  const computerChoice = getComputerChoice();

  console.log("You threw:" + userChoice);
  console.log("The computer threw:" + computerChoice);
  console.log(determineWinner(userChoice, computerChoice));
};

playGame();

===============================
Note: I removed the break statements because it’s unnecessary here, it will never be reached because return will stop the function.

switch (randomNumber) {
    case 0:
      return "rock"; //end the function and return "rock"
      break; //unreachable code so just remove it

It works!!! i feel so relieved, i almost gave up and moved on, thank you so much for your time and exactly detailed explanation.

1 Like

Thank you so much for your time.

you are welcome, never give up you are doing great work! :boom:

1 Like