Rock, paper, scissors

Hello,
I am having a hard time with this project I keep getting errors but I am following the walkthrough.
Here is my Error message:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:31
if( userChoice === ‘paper’) {
^

ReferenceError: userChoice is not defined
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:31: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

and here is my code:

const getUserChoice = 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!";
  }
}
console.log(determineWinner('rock', 'scissors'));
console.log(determineWinner('paper', 'scissors'));
console.log(determineWinner('rock', 'rock'));

What am I missing?

Thank you!

Hi,
if you format your code according to this guide and use indentation, you’ll probably see that the line in question is out of the function’s scope.

this code:

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!";
  }
}

is after/outside the determineWinner function, why?

Thank you for that .

1 Like

The formatting was done by @stetim94 . I can‘t do that in someone else‘s code. But using the guide should help you debugging future posts. Did you get your code to run now?

1 Like

i had to woke the last two days so now I can sit with this and try it out.

thank you

I’m not sure to be honest, I followed step by step in the video 3 times and i kept getting errors.

thanks for your help

That worked for me. Thank you!

1 Like