Zero-Based Indexing Hint: Remember, arrays look like this: var arrayName = [1, 2, 3];


#1

Hello all,

I have a question about the hint given in Loops and Arrays I.

Step 4 states:

The for loop runs until i < 4 (because cities.length equals 4. The array cities has 4 elements in it; see the Hint for more.)

I figured I probably knew what the Hint would say, but went to check:

The length of the cities array is 4. But when we are accessing the array, the last index position of the array is array[3]—zero-based indexing! Keep this in mind!

I understood this part, but below that was this reminder:

Remember, arrays look like this:
var arrayName = [1, 2, 3];

Should this be var arrayName = [0, 1, 2, 3];?
If not, can someone explain to me what I am missing here?

Thank you,
frisby

https://www.codecademy.com/courses/javascript-beginner-en-NhsaT/2/3?curriculum_id=506324b3a7dffd00020bf661#


Replace this line with your code.


#2

I guess the hint is just to show the array syntax.

var arrayName = [x,y,z,...]

About the zero-based indexing well it goes like this:

var arrayName = [1, 2, 3];
arrayName[0] -> 1
arrayName[1] -> 2
arrayName[2] -> 3

so you start counting your indices from 0 and not from 1


#3

Ah, I see; that makes so much sense! It seems I've lumped it in with the previous paragraph when I shouldn't have. Whoops!

I still think it would've been less confusing to have the array start with 0 because of the similar numbers in the two hints (3 and 3) (unless the example's expanded as you've done), but I digress. :grin:

Now that I know, I'll read more carefully when I see "look like ~this~" next time and hopefully don't get confused.

Thank you for your help and quick response, haxor789!


#4

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.