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
Ernie Smith8,511 Points
Confused about how the first computer guess is made to start the loop
In a while loop the code inside the code block does NOT run unless the condition is true . But when did the computer first make a wrong guess to test the condition?
It seems like var guess has no value (number) to test until guess = getRandomNumber (upper) is run? But/ so the code block should never start? This is what has me perplexed.
Basically to me it looks like the loop should never run because "guess" has no number to compare to the randomNumber. Why is a do loop not necessary? So confused.
The variable 'guess' initially does not have any value when created. So, when you look at the beginning of the while loop you will see 'while ( guess !== randomNumber )'. What is located in the parenthesis is important. Although the variable 'guess' does not have a value, it is still NOT EQUAL TO (!==) randomNumber, which returns as true. In simplified terms, when 'guess' IS NOT equal to randomNumber the condition is true and the loop will run, but when guess IS equal to randomNumber the condition is false and the loop will stop. Whenever the condition is true inside of a while loop, the code inside the code block will run which is what you see inside this program.
Other languages you would need to establish the variable before the condition statements.