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
Enda Sugrue
547 Pointswhile command isn't working
When ever I try to run :
while(true) {
console.log('Hello World! ')
}
When I open the html page in chrome it keeps crashing or becoming unresponsive.
The rest of the code runs fine.
2 Answers
Florian Goussin
8,925 PointsYou're not supposed to use a while loop with no exit condition. Otherwise the while loop will loop endlessly. That's why it may crash.
Just use console.log("Hello World!") alone or in a while loop like that:
var loop_counter = 0; while (true){ console.log("Hello World!"); loop_counter++; // add 1 to loop_counter if (loop_counter == 10) { break; // quit the loop } }
A better practice is to not use while true and rather specify a condition (while loop_counter < 10 loop again and again) for the while loop to stop :
var loop_counter = 0; while (loop_counter < 10){ console.log("Hello World!"); loop_counter++; }
Enda Sugrue
547 PointsThanks for your answer. I just following the loop tutorial and it was not working for me.
Thanks