JavaScript Treehouse Club - MASH MASH - JavaScript MASH Choice and Random Number Functions

Elliot Denvir
PRO
Elliot Denvir
Pro Student 1,732 Points

Is Num defined?

I am confused as to where the variable num is defined. At what point during the code or the game is num given a value, except for in the line var num = num || 4. And if this is the only point then will num always equal 4?

Thanks

Elliot Denvir
Elliot Denvir
Pro Student 1,732 Points

I have continued working and is this the line which defines num? var randomNum = random_number(4); So num is hardcoded to take 4?

1 Answer

Erik Nuber
Erik Nuber
20,627 Points

I haven't been thru the this particular lesson but, I looked at the code.

The function random_number is called on both line 10 and 27 of the initial JS file.

on line 10 4 is also passed into the function so num would be set to 4 but, on line 27 it says

return choices[random_number(choices.length)];

this means that the random_number function is being called and num = choices.length in this case. So num doesn't always equal 4, it can be equal to the number of items in the choices array within the final function get_answer.

It looks like this is done because a user can enter up to four choices so num could be equal to 1, 2, 3, or 4