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

Very confused by this question...

Hey team, I was doing a challenge and answered by placing a condition inside of the parenthesis: while ( counter < 10 ).... It was incorrect. Why, what did I miss or do wrong? Thanks!

Here is the challenge:

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?

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

2 Answers

Steven Parker
Steven Parker
241,811 Points

Since the counter starts at one, the condition "counter < 10" will only run nine times. To run ten times the condition should be "counter <= 10".

It would also work with "counter < 11", but the intention would not be as clear.

those off by one errors!

Steven Parker
Steven Parker
241,811 Points

Computers can be so picky! :smirk:

Happy coding!

Thank you, this loop lesson is a tricky one for me!

Thanks Steven!!!