Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
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.