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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Traversing Elements with children

Jimmy Names
Jimmy Names
10,371 Points

Within the For loop why not <= rather than <

If we wanted to use /each/ iteration of the .length (including the last one) then wouldn't 'less than and equal to' make more sense as it would cycle through all nodes rather than the one before .length is reached?

I know I[m probably wrong but some clarification would be nice.. tytyty

2 Answers

Todd Milkey
Todd Milkey
22,892 Points

The iteration is zero based, therefore the first one is actually zero not 1. The last one would then be .length -1.

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hey,

Here is the logic here.

< Typically means I want a number up to but no including.

<= means I want all numbers up to and including.

If I were to iterate through all the items with < I would have to go up a number. 1 < 5 means when I get to 5 do nothing. That means I am missing 5 so I would do < 6. Which would do it. If I use 1 <= 5 I am saying do all things up to and including 5.

There is always more than 1 way to do something. When it comes to operators and such, there are no real hard fast rules. Just do what makes sense.