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 trialAnna Gibson
Front End Web Development Techdegree Student 4,468 PointsWhat's wrong with my loop?
For all intents and purposes it appears my loop is fine can't seem to pick out what the problem is. It keeps saying that it's 'taking too long to load'.
var count = 0;
while (count <= 26) {
document.write(count);
}
2 Answers
Matt Socha
6,541 PointsYou need to increment count
Since count
is never increased its always going to be 0, which meets the condition of the loop.
Therefore the loop never ends.
add count++
inside the loop
andren
28,558 PointsThere are two issues with your loop:
You have created an infinite loop, that is why it's taking too long to load. The loop will run as long as
count
is less than or equal to 26, and there is nothing in your code that increases the value stored incount
, which means it will never change from its initial value of 0.Your loop will end up running 27 times, not 26. This is because
count
starts at 0 not at 1.
If you add code to increase count
by one each loop, and change the condition to count < 26
, which will be 26 times when you start counting from 0. Like this:
var count = 0;
while (count < 26) { // Loop while count is less than 26
document.write(count);
count++; // Increase count by 1
}
Then your code will work.