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 The Refactor Challenge Solution

Justin Warren
Justin Warren
7,805 Points

Why does he start the counter at 1 instead of zero?

Could he have started at 0 and set the condition to i < 10? Or was there a specific reason he used i = 1 and i <= 10?

Thanks!

3 Answers

nico dev
nico dev
20,364 Points

Hi Justin Warren ,

Yes, like you said, he could have effectively made it like that, and it would have worked. Both of these do the same thing:

(var i = 0; i < 10; i++)
// or
(var i = 1; i <= 10, i += 1)

The only difference I see, though, is that later if/when things start getting a little bit more complex, and maybe you want to use or interact specifically with some of the items in that cycle or whatever, if you do it the second way, the value of i will be the number of the item you're going through. Although now dealing with divs and colors that probably doesn't make a lot of sense, |o| but later in this course, if I'm not wrong, you''l deal with students and their data, when you're there remember this and you'll see what I mean. If i represents students in a list, rather than just pure cycles or divs, when i is 3, for instance, that means you're dealing with the third student in the list, whereas with the other method, because you've started at zero, i would probably be 2 instead in the third student.

A little bit different to put it into words, but what I mean is, when i's value is 3, you're effectively dealing with the third item in the cycle, as opposed to the first way to do it, where you're going to be dealing probably with the index number (if loping through an array, for example) but not with the 'sequential order' number, if that makes any sense.

Daniel Salvatori
Daniel Salvatori
2,658 Points

So if I wrote it like this

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

would it not work at all? or would it just give me 11 Divs instead of 10?

it would work. that would give you 11 div, which is why in the video the counter was set a 1. 1-10 if it set the way you have it 0-10