Functions inside Functions FunctionCeption


Why do I have to put the toLowerCase() function inside the getUserChoice function? Why can’t I place below the function? When I placed it below the getUserChoice function it said userInput is not defined… What is the logic behind this?

Well, try and answer these questions, and see if you can figure it out ;).

What would be the reason you use a toLowerCase at all?

Why would userInput return a not defined?

Well, I use toLowerCase() in case people put ROCK PAPER SCISSORS or any variants of rock, paper, scissors… that way I don’t have to account for each type of capitalization…

Is userInput only defined through the function?

Thats spot on, so, where would you use this log, inside the function getUserChoice or outside of it?

I give you a little hint, function parameters versus variables :wink:

You’re right; I’m trying to figure out the difference between them now… I just realized that getUserChoice is a function not a variable.

Function parameters are defined in the function… ha

Thanks so much! This is exciting!

Actually getUserChoice is a function which takes one parameter, “userInput”.

When you call this function

getUserChoice('PAPER');

The string “PAPER” is passed into the function as the userInput parameter. The function will then work with this value stored in the variable userInput, this variable only exists within the scope of the function. That is why userInput does returns a not defined when called outside of the function.

The toLowerCase() makes sure that it is ready to be run through the if…else if…else statement.

1 Like

Yes, because there is nothing declaring userInput outisde of the funciton. There is not variable userInput outisde of the function.

Great description!
Thank you :slight_smile:

1 Like