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

Sravanthi Kota
Sravanthi Kota
425 Points

how do i write the code

what will be the correct answer?

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

2 Answers

Almost! You have > in the loop's condition, which keeps the loop from starting (as 1 > 10 is false). You need <=

while (counter <= 10) {
    document.write("<p>Now in loop #" + counter + "</p>");
    counter += 1;
}
Juan Manuel Romero
Juan Manuel Romero
5,293 Points

In order to iterate over the loop, the condition inside the while statement should be

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

}

because in that way the number variable will satisfy the condition until it turns to 10 , the error that you have is that since 1 is not greater than 10, the code inside while statement will never get executed.