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 For Loops

why "<=" instead of just "<"

I get that there may be situations where "<=" is warranted, but this does not seem like one. We're just printing a specified number of divs to a page in a for loop

I get that there may be situations where this may be warented, but this does not seem like one. We are just printing a specified number of divs to a page

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey John,

In my opinion, this would be an very good situation to use <= instead of just <. Because you are printing a specific number of DIVs, it is much easier to read that in the code using <=. The variable starts at one and the loop will run as long as the variable is "less than" or equal to 10. So, you would get 10 DIVs (1, 2, 3, 4, 5, 6, 7, 8, 9, 10). If you only used the < then you would only get 9 DIVs, because 10 is no longer "less than" 10. So, to get the same result, you would have to change you code to

i < 11

While this will produce the same result, I think this would be more confusing to read in code because you're not outputting 11 anythings, but rather everything less than 11. I think seeing a 10 when you want 10 things is just a bit more clear.

Both would be correct. This is just my opinion on it. :)

:dizzy:

Jason, thank you for your response. So by my count, this code:

 (i = 0; i <= 10; i += 1)

will produce 11 divs. I thought we wanted 10 divs. Maybe I missed something, I often do :D

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Sorry, I didn't have that clear in my answer. I was referencing the example in the previous video where it was i = 1. I should have had specified that, however.

If you were to initialize the i variable to 0 (Zero), then yes, you would get 11 iterations through the loop. In that case you could use just the < sign. But, again for reference for a future you or another coder, in this example, the best bet would be to leave the <= and initialize the i variable to 1.

This way when you quickly look at the code you see 1 'to' 10.

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Hey John,

All good. I fixed the first comment for you and deleted the other one. Your markdown was close, you just needed 2 hard returns before the code block and closing ```. :smiley:

Thanks Jason, that clears it up.