Rock, Paper, or Scissors: ReferenceError

Here is the link to the exercise: https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript .

I have performed a few steps and I am on the step 6 to test the first “getUserChoice” function.

Here is my code so far:

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

getUserChoice(test); 

The error I get: /home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:10
getUserChoice(test);
^

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

Here is the screenshot of the error if needed: https://prnt.sc/p2qaj3

Am I calling the function differently from what it needs to be done?

Also, I suppose there is an issue with how I use the “toLowerCase()” method. This method is to be used with strings. And “userInput” is a parameter here in the code.

1 Like

Hey @nastya_nmk

Where do you define the variable test? It doesn’t look like you ever made a variable by the name of test?

Also, why do you return a console.log statement? You don’t need to do that. If you want to have a return statement, you can delete the console.log part or if you only want to log that text to the console, you can just remove the return keyword.

4 Likes

Hello, thank you for the reply.

I didn’t define the test variable because as I understand it’s not necessary to declare it for the function to be called.

As I understand, when I call the function: getUserChoice(); , it works in the background as getUserChoice(userInput); So, if the code works correctly, then if the word ‘rock’ is used by a user: getUserChoice(rock) , I will see ‘rock’ in the console(?) .

And if I use getUserChoice(test) , it should return the message: Error (?)

Also, I used the console.log from the instruction: https://prnt.sc/p2r6jc .

I think I understand that I can omit the use of the return message. When there is no “return” word that is next to the console.log, here is the error message I see:

/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:3
userInput = userInput.toLowerCase();
^

TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:3:24)
at Object. (/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:11: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)

I also see the same message both, when the return word is present next to the console.log and I use getUserChoice(); and when the return word is not present and I use getUserChoice(); (without any words in the parenthesis)

2 Likes

a function consists of two parts, the function declaration:

const getUserChoice = (userInput) => {

the function has a parameter userInput, which acts as placeholder until you call the function:

getUserChoice('argument value');

when calling the function, you provide argument(s) for your parameter(s).

If the argument is a variable, the variable needs to be defined. You can also pass string/numbers as argument.

what you said here, applies to parameter(s), which as explained, act as placeholder until they (parameter(s)) get there value from argument(s) at function call

5 Likes

Thank you.

I have tested it this way at the end: console.log(getUserChoice(‘paper’)); and it works.

3 Likes