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

When does the program know when to "break" when there's no "while" counter?

I am reading through this code that Dave types in:

while ( true ) { guess = prompt('I am thinking of a number between 1 and 10. What is it?'); guessCount += 1; if (parseInt(guess) === randomNumber) { correctGuess = true; break; } }

and I cannot understand WHEN the program knows to break since there is no "while (counter < 10)" statement. You can view the full code at about 3:30 min into the video.

1 Answer

Hey, the program will break base on condition, when the number you input is equal random number, it will break. Or it will run infinite until you guess the correct number.

Thank you. I don't understand why anyone would create an infinite loop like that.