Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Elliot Denvir
PLUS
Elliot Denvir
Courses Plus 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
Courses Plus 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,629 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