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 Create a while Loop

Andrew Nicholson
Andrew Nicholson
2,474 Points

My code took too long to run (??)

Is this because my code is wrong or because my internet speed is horrendous?

script.js
var count = 0;
while ( count < 26 ) {
  document.write( count + " ");
}

2 Answers

Dan Johnson
Dan Johnson
40,532 Points

You forgot to update count in the while loop, so count < 26 will always evaluate to true, locking the program in an infinite loop.

Andrew Nicholson
Andrew Nicholson
2,474 Points

ah very good! I couldn't for the life of me work out if there was something missing or not. Code blind!

Thanks

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,252 Points

Yup, the loop needs a way to break out. You need a way of changing the value in the code so that eventually the while condition will be false ensuring the loop will end!. :-)