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 Complete the Loop

Document.write problem

The document.write method is called undefined times

script.js
var counter = 10;
while ( counter < 10 ) {
    document.write("<p>Now in loop #" + counter + "</p>");
    counter += 1;
}

2 Answers

Charles Kenney
Charles Kenney
15,604 Points

James McIntyre,

You've initialized the counter variable at 10 and wrote a while loop that will only run while the counter is less than 10; as a result, the while loop's block will never be executed. In order to get the loop to be executed 10 times, the counter should be initialized at 0. Your code should look like this:

var counter = 0;
while ( counter < 10 ) {
    document.write("<p>Now in loop #" + counter + "</p>");
    counter += 1;
}

Hope this helps,

Charles

Justin Radcliffe
Justin Radcliffe
18,987 Points

Hi James,

You've initially set the counter variable to 10, so your condition (counter is less than 10) will never be truthy. You need to reset the counter variable to:

var counter = 1;

And your while condition should include:

while(counter <= 10) {
...
}