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

Simon Chan
PLUS
Simon Chan
Courses Plus Student 10,577 Points

What happens if the guess variable is declared inside the while loop? Will the loop run for the first time?

Will the loop be run for the first time when the variable "guess" is not declared and how is the condition checked?

And, assuming the loop runs for the first time, will the "guess" variable be redeclared every time it runs?

while ( guess !== randomNumber) { // value of guess is ? var guess = getRandomNumber(upper); // value of guess is a random number generated attempts += 1; }

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

If you declare it inside the loop you'll get "Uncaught ReferenceError: guess is not defined" in the console and the loop won't run.