I need help on the Rock, Paper, Scissors project

Okay, so I started doing the project and I keep getting this error, heres the code:
const getUserChoice = (userInput) => {

if(getUserChoice === ‘rock’ || ‘paper’ || ‘scissors’){
return userInput();
} else {
consol.log(‘Sorry, you can try to cheat but I wont let you!’)
}
};
userInput = userInput.toLowerCase();

getUserChoice(‘rock’);

I thought it was the location of the userInput.toLowerCase(); so I moved it here

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();

to no avail, it keeps giving me a “userInput is undefined” error message. I tried separating it into a function instead of the way they asked and I still got errors. If anyone knows what I am doing wrong and can help me I would appreciate it.

Thanks

Refer to the parameter, not the function name.

if (userInput === ...)

Another problem is the operands in the logical expression equate to one value. The three expressions need to be written separate…

if (a === 1 || a === 2 || a === 3)

Oh, lol I knew it was something super simple and I just wasn’t getting it.
Thankyou very much!

1 Like

Alright I thought it was fixed but I am still having problems. Here is the code, now it says userInput is undefined, userInput = userInput.toLowerCase(); and return userInput are wrong as well. I have been messing with this for nearly 45 minutes and I can’t get this one block of code to work, please help

const getUserChoice = (userInput) => {
userInput = userInput.toLowerCase();
if(userInput == ‘rock’ || userInput == ‘paper’ || userInput == ‘scissors’){
return userInput();
} else {
consol.log(‘Sorry, you can try to cheat but I wont let you!’)
}
};

userInput(‘rock’);

Here the reverse is true. Use the function name in the call, not the parameter.

getUserInput('rock')

For comparisons in JavaScript, use the strict type matching operator for identity, ===.

When I did that the other two errors would show up…
Thanks for being as patient as you have been :slight_smile: I’ll fix the current problem and see if I can find out what is wrong with the others lol

Another problem showed up when I changed it… I feel like maybe scrapping and restarting would be a good idea lmao.
It now says
/home/ccuser/workspace/javascript_101_Unit_3/Unit_3/rockPaperScissors.js:3
if(userInput == ‘rock’ || userInput == ‘paper’ || userInput == ‘scissors’){
^

ReferenceError: userInput is not defined
at Object.

Let’s go back to your opening post example.

const getUserChoice = userInput => {
    return userInput;
};

console.log(getUserChoice('rock'));

That’s how I start every function. The basic function block with parameter, and the call afterward.

If it logs 'rock then we know the function is working. Now fill in the missing bits, one by one and test again.

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  return userInput;
};

console.log(getUserChoice('ROCK'));

The logged value will again be, rock. Now add the conditional…

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
    return userInput;
  }
};

console.log(getUserChoice('ROCK'));

Again, rock should be logged. Having got this far, we can add the else branch…

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
    return userInput;
  } else {
    return false;
  }
};

console.log(getUserChoice('stone'));

This time it should log, false.

Okay, I think I get it. It seems I tried to do too much at once and caused a lot of problems in my code. Next time I will slow down and take it step by step. Thank you for the help! I really appreciate it!
If I have any problems down the line I will let you know :slight_smile:

Have a good one!

1 Like