Why won't my code work for the Rock, Paper, Scissors Project

Rock, Paper, Scissors Project:

Why won’t my code work?! It did work with the first two if blocks and console log of function with parameters rock and paper, but then when I added more, none of it worked anymore. I tried commenting out the new code and old code no longer worked!

Hey there @course9212752744! Welcome to the forums :slight_smile:

I tried running your code, and I got an error indicating there was something wrong with your conditional statements.

And what you’re doing with your code (in the determineWinner() function) is something like this:

if (userChoice === 'rock') {

      if (computerChoice === 'paper') {

        return 'You lose!';

      } else {

          return 'You win!';

      } else if (userChoice === 'paper')

Your else if statement is nested inside your first if statement, therefore it will only check if the userChoice is ‘paper’ if userChoice is ‘rock’. Doesn’t make much sense does it?

So what you’d have to do is close each conditional statement(if or else if) that checks the userChoice before starting a new one. That way they won’t be nested and you won’t get errors.

I really hope I made myself clear, if you still have any questions please tell me.

Hi I am having similar issues with the Rock, Paper, Scissors project.

everything runs smooth until step 11 when I console.log i get errors… also when i watch the walkthrough he types the console.log like this - console.log(determineWinner(‘rock’, ‘scissors’));
however with my console i have to place \ to escape special keys… above all when i try to log to the console i get errors???

https://gist.github.com/7414968f797cea412715a0e7c3ffbd6b — Here is my code.

Please copy and paste the errors that you are encountering so that we can better assist you.

Hey,I actually managed to solve the issue in the end but thanks for getting back to me and the tip!

Thanks again,

David

1 Like

I’m having similar trouble with my code.:

const determineWinner = (userChoice,computerChoice)=>{

if (userChoice === computerChoice){

return ‘This game was 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'

}

}

};

console.log(determineWinner(‘rock’,‘paper’));

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

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

Is your if statements nested inside of the const determineWinner?

Yes, the if statement is the first statement withing the {} of the const determineWinner. The console.log(determineWinner(‘rock’,‘paper’));outside and after the const determineWinner. Sorry for the terrible copy-paste, that was my first post and I didn’t realize the code would look like gibberish.