Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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) {
...
}