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 Exiting Loops

The randomNumber variable does not declare first in this video.

Hi,

I am confusing that why the "randomNumber" variable inside the if condition can run without declaring the variable first outside the loop like var randomNumber. I know it might works with the getRandomNumber function but the two names are different. Can someone explain in a little more details? Thanks for your time.

if (parseInt(guess) == randomNumber) { correctGuess = true; break; }

1 Answer

Colin Marshall
Colin Marshall
32,861 Points

The randomNumber variable is declared outside the loop. It is declared on line 1. If you comment out that line, it will not run anymore because randomNumber is not defined. Hope that helps!