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

Ryan Schmelter
Ryan Schmelter
9,710 Points

At what point is the function called here?

Here the math.random getRandomNumber function needs to be called twice in each loop - once for the random number (A) and once for the random number guess (B). When is (A) called in this code? It's working, but I can't figure this out. Is the code inside the parentheses while ( ) run by just listing the variable?

Steven Parker
Steven Parker
229,921 Points

Please show your code so we can understand the issue. Remember to use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.

Yes. Remember the javascript interpreter is looking at

var guess;

and comparing whatever value is stored in it (which in the beginning is nothing at all) with the value stored in the variable "randomNumber", which is generated by the function :

var randomNumber = getRandomNumber(upper);

The code block in the while loop is telling the computer to run that same function in order to keep assigning a value to the "guess" variable UNTIL the condition statement in the while loop is no longer true. In other words, when the value in "guess" (finally, after however many attempts) equals the value originally generated in the variable "randomNumber".

Does that make sense?

Evan Trimby
Evan Trimby
5,381 Points

I'm guessing you already got this since it's been so long. But the function is called once on line 2 when the variable randomNumber is created. This is the equivalent of you picking a number then telling a friend that you are thinking of a number between 1 and 10. The loop which calls the function on line 11 for each iteration of the loop is the equivalent of your friend guessing over and over until they get it right.