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 JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Blaize Lange
Blaize Lange
2,298 Points

Is the first part of the code var randomNumber = getRandomNumber (upper); call to the function below?

Does it call to the function below it to generate a random number that is stored in the var randomNumber? Then the guess calls to the same function to keep looping until guess === randomNumber? Am I interpreting that correctly? I did not know variable could call to function below it because I thought it is read top to bottom and if it reads the var randomNumber that calls to a function that it has not read yet it wouldn't store anything. Does the script after reading the function go back to store a random number in the var randomNumber?

2 Answers

andren
andren
28,558 Points

Does it call to the function below it to generate a random number that is stored in the var randomNumber?

Yes, that is what it does.

Then the guess calls to the same function to keep looping until guess === randomNumber?

Yes, that is also correct.

Am I interpreting that correctly?

Yes, your understanding is correct.

Does the script after reading the function go back to store a random number in the var randomNumber?

This is actually a bit harder to explain, it's true that JavaScript files are generally read from top to bottom but before JavaScript starts executing commands it first does something often refereed to as Hoisting. Exactly what hoisting does is a bit complicated and hard to explain, but in essence JavaScript registers all functions and variables that exist in the script before it actually starts to execute the commands from top to bottom.

Because of this you can in fact use a function before it is declared, you can also technically reference variables before they have been declared in the file, but doing so will result in JavaScript returning the value as being Undefined, since it only registers that the variable exists. It does not assign it any value until it actually reaches the code that declared the variable, therefore you should never use a variable before it is declared. Functions on the other hand are safe to use earlier in the file than they are declared.

My explanation of hoisting is of course a simplification, If you want to understand hoisting on a deeper level then I recommend Googling it. There are some pretty informative articles about it on the web, but since it is such a complex topic going into a more detailed explanation is beyond the scope of this post.

Blaize Lange
Blaize Lange
2,298 Points

Thank you for going into so much detail!

Jason Welsh
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jason Welsh
Treehouse Project Reviewer

Hi. I can't remember the code in that one exactly. If you can post the code here I will be happy to answer your question!