JavaScript Treehouse Club - MASH MASH - JavaScript Handle Submission and Event Handler Functions

Marilou Lekanne Vaquer
PRO
Marilou Lekanne Vaquer
Pro Student 1,181 Points

i don't fully understand what it means to 'call a function' . For example with : function random_number (num).

I understand that the function is like engineering the accelerator and calling the function is telling the program on how to use it (pressing the accelerator with your foot). However, in the example of random number, wouldn't you have to put in all possible answers when 'calling the function' ?? That would be a lot of numbers, so that's obviously not correct, but I don't understand how giving for ex the value (46) would be considered calling this function and have it perform the way it should.

1 Answer

Joseph Wasden
Joseph Wasden
20,371 Points

tl:dr; Try to separate in your mind the name of a thing, and the thing itself.

imagine you have a button to start your car. but before that button can be pushed, you have to have the key inside the car. that function would look something like this.

function aPushButtonIngnitionCar (wirelessKey) {
   //all the code that makes the ingnition work
}

Where wirelessKey is at, that is something we require to run the function. it refuses to let you push the button otherwise. the actually name in the parentheses doesn't matter. I could have put bunny rabbit. the point is, SOME kind of value must be present to proceed. or else, the button won't push, because the car requires SOMETHING (also known as a parameter) before it can run. SOME kind of key is required for each ingnition sequence.

Now lets look at the random number. random number requires a parameter of num. Now, that's just a handy label so we can reference that parameter in the function itself if we need to. but it could say bubbleFruitsZappyZap and we would still need it. But that isn't very helpful of a name, is it? the num is used to give you a clue that a number is desired. for every time you push the random_number button, you have to first provide a num, or, as the name suggests, a number.

function random_number (num) {
   //insert your logic to be executed here
}

hope that helps some. ask more questions if you need.

Marilou Lekanne Vaquer
Marilou Lekanne Vaquer
Pro Student 1,181 Points

thank you so much Joseph for your time and explanation. It certainly makes more sense now.