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

phillip smith
phillip smith
1,681 Points

whats the answer and why

whats the answer and why

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

1 Answer

Steven Parker
Steven Parker
229,732 Points

A "while" needs a conditional expression between the parentheses, and the one here has nothing but a space there.

To make it work correctly, you need a comparison using "counter" that will be true until the loop body has run 10 times.

phillip smith
phillip smith
1,681 Points

Thank you for responding. So what would the counter comparison be?

Steven Parker
Steven Parker
229,732 Points

A typical expression would be "counter < some_value" — I'll leave it up to you to figure out what "some_value" should be to make the loop run 10 times. :wink: