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 A Closer Look at Loop Conditions

Document.write was displayed 27 times, not 26?

Well, I answered the question; however, I'm wondering why? The puzzle required me to write document.write 27 times.

var count = 0; while (count <= 26) { document.write("We're currently on set # " + count + "."); count += 1; }

But when I entered the operator as <= it gave me 27 document.write instead of 26? Why? I solved the puzzle by replacing "<=" with "<". But shouldn't it still be 26? Less than or equal to 26... Why was it improper to use (count <= 26) instead of (count < 26) is my question.

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Dennis,

You have remember you are starting your count at Zero and not at One.

So when you have <= it means less than OR equal to, so the loop will stop at, but include 26, and zero to 26 is 27 times. When you changed it to just < it will stop at, but not include 26 (because 26 is NOT less than 26).

I hope that makes sense.

Keep Coding! :)

Jason is right, you always have to watch how you handle off-by-one and fencepost conditions.