Rock paper scissors game

Hello everyone, so I’m having a bit of a problem with the Rock, paper, scissors game. Below is my code and at the end of it I expect to have ‘Rocks’ as my User input but i keep getting ’ Error!, Undefined ’ no matter what i input. What am i doing wrong?

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

then the first debug step would be to log the value of the variable (which we use in the comparison), to get an idea what might the problem:

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

do you see what the value of userInput is?

Yes, but i don’t see a problem with it. why does it give me Error

userInput now holds a function. You can see this by the .log() I added

How is the function suppose to equal a string?

I don"t understand what you mean.

I’m thinking the value of userInput sholud be either ‘rock’ , ‘paper’ or ‘scissors’ . If its not then it should prinT ‘Error’ . Isn’t that the purpose of the ‘if’ statement?

I’ve figured it out. Wrote the code again from scratch and got the right response. must have been a syntax error.
thank you very much

you forgot to call/invoke the toLowerCase function, so your variable now held the function itself, and not the returned result of the functions execution

1 Like