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

Please help with this challenge...

simply repetitive task with loops

Challenge Task 1 of 1

This is a nearly complete while loop, but something is missing. The loop should run 10 times, but it's not working at all. Can you fix it?

I cannot figure this one out... please help.

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

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Bobbi,

You're very close! The reason you're not able to pass the challenge is because the loop is only iterating 9 times. Since your counter begins at 1 and you are only looping until the counter is less than 10 it is resulting in:

1, 2, 3, 4, 5, 6, 7, 8, 9

If you change the counter to begin at 0 or change the condition to be:

while (counter <= 10)

...then you'll have 10 iterations for the loop.

If you change the counter to 0:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9

or if you change the while condition to what I have above:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

I hope this helps.

Thank you Justin, I tried <=10, but it gave me a syntax error, so I tried <11 and it worked!

thanks for you answer ;) Your explanation helped me figure it out.

while (counter < 11)