Rock,Paper,Scissors

I’m not quite sure what is wrong with my code. I’ve tried following the walkthrough video and I’m still stuck. I keep getting an error code that says ‘ReferenceError: userChoice is undefined.’ If anyone could help I would greatly appreciate it. Below is my code

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);
switch (randomNumber) {
case 0:
return ‘rock’;
case 1:
return ‘paper’;
case 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 ‘The computer won!’;
} else {
return ‘You won!’;
}
}
if (userChoice === ‘scissors’) {
if (computerChoice === ‘rock’) {
return ‘The computer won!’;
} else {
return ‘You won!’;
}
}
console.log(determineWinner(‘paper’, ‘scissors’));

Then you would think about when/where that happens and ask yourself if it should be defined, if yes make sure that it is defined at that point, otherwise don’t use it.

It’s probably better that you don’t refer to a finished solution and instead think about what’s happening in your code and what you need to be happening differently for it to do what you want.

And if you don’t know what should be happening then you better figure that out before you write any code, because nothing other than you will make it behave how it should.

I know where it’s happening I just don’t know what I’m doing wrong. It’s happening in this section.

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

What makes you know it’s there? I doubt it’s there, it’s defined there.

Typically an error message like that comes with a line number to tell you where it was. A bit crude but you can also use console.log to narrow it down if for some reason you do not get a line number.

You can find what’s wrong by identifying where the incident happens and work your way backwards through what happened before it.

If I run your code I get an error message like this:

/tmp/derp/js/a.js:25
if (userChoice === 'rock') {
^

ReferenceError: userChoice is not defined

And if you look at that scope and any surrounding scopes, there is indeed no definition of that variable.

That still doesn’t say what’s wrong, but this is where you’ll need to compare that situation to what you meant for that to be.

Likely the situation you’re looking for is something like having called determineWinner which defined this variable in the scope of that function and that this code would run inside that function. That’s not what’s happening, but it gives you a couple of things you can investigate.


If you’re still not able to find it then you may want to do your debugging with LESS code. Start (temporarily) removing things. You can for example remove all other functions/code, and you can use fewer cases, you don’t need to compare to all of rock/paper/scissors, you can use just one while you’re debugging. This is now something like 10 lines to look at. You can even remove all comparisons, leaving you with 4 lines.

I believe it’s in that section because everything before that works perfectly fine. I added the rest of the code after that section to see if I was missing the rest of the code and it obviously didn’t work. Error code is telling me line 25 which is the section I said I was having a problem with.

The code you posted defines that variable.

25 is further down, far enough down that your variable no longer exists, which is what the error message is saying.

And, again, removing things lets you consider less code.

ohhhhh okay, I get it now. Thank you for your help.