Rock paper scissors project

I wrote out all of the code for this this once before, It worked fine while I was writing it up until the last function of determineWinner. I looked through my entire code a few times for a few hours total and I didn’t see any errors, yet I kept getting the same error message;
TypeError: Cannot read property ‘toLowerCase’ of undefined
at getUserChoice

So I decided that since its a really short code I would just delete what I already wrote and redo it. So, now I have started to rewrite my code. I wrote the first function and it worked great, initially. Now, when I write the second function I’m getting the same error posted above. I have found a solution for the project and the code they had written out is the same as mine and produces the same error. Any help would be greatly appreciated.

here’s my code:
const getUserChoice = userInput => {
userInput = userInput.toLowerCase();
if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissors’){
return userInput;
} else {
return ‘Please check your spelling and try again!’;
}
}

const getComputerChoice = computerInput =>{
Math.floor(Math.random() * 3)
}

userInput = ‘rock’
console.log(getUserChoice())

1 Like

You call getUserChoice without an argument, so the parameter userInput is undefined.

Ok thank you for your speedy reply. So I must have misunderstood what I was reading while learning functions. Throughout the previous lessons the syntax I learned was function thisIsAFunction(parameters). I don’t believe they had me wrote any like the one I did here, I got to this point by reading and understanding what the step was ask in for. I should probably try and write it the way I know how to, but I’m not sure that would work either.

So when I wrote:
const getUserChoice = userInput {} and the block of code between the curly brackets what exactly does that mean.

What I currently think it means from the resources I have is that getUserChoice is a constant function that can’t be altered. That getUserChoice receives an input from userInput which in his case is a string that has four different possible values: rock, paper, scissors, or an error. I’m obviously mistaken somewhere in either my logic or execution. Do you have any good resource recommendations where it goes more in depth for beginners? Is my logic of how this function works completely wrong, if so not too much to ask, can you try and explain the way it works? I am trying to visualize how this works on a functional level, like when i put a pencil into a pencil sharpener and roayqye the arm, a series of cylinder shaped blades rotates around the tip shaving off the pencils wooden outside exposing the led and shaping it to a point. That’s a really basic analogy of the type of information I’m looking for.

The way you’ve done it, all you have to add is the argument:

const getUserChoice = userInput => {
  return userInput
}
userInput = 'rock'
console.log(getUserChoice(userInput))

The parameter of a function will always be ‘undefined’ unless you pass an argument to the function call:

const getUserChoice = param => { return param }
console.log(getUserChoice()) // undefined
console.log(getUserChoice('rock')) // rock
const userInput = 'scissors'
console.log(getUserChoice(userInput)) // scissors

This is the best way of doing it, but you can also call the function without an argument like this:

userInput = 'rock'
const getUserChoice = () => {
  return userInput
}
console.log(getUserChoice()) // rock

If you have a function with a parameter, this parameter will always be the argument you passed or undefined whether you declared a viariable with the same name or not:

userInput = 'rock'
const getUserChoice = userInput => {
  return userInput
}
console.log(getUserChoice('scissors')) // scissors
userInput = 'rock'
const getUserChoice =() => {
  return userInput
}
console.log(getUserChoice()) // rock