"for" loops with arrays and the property (some variable).length; NEED HELP?


#1



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


I correctly provided the code necessary to advance to the next exercise. However, I am a bit confused as to why I cannot say that my array is less than or equal to my for variable. Apparently, names.length, my array variable name being accessed with a global JavaScript property, has to be less than my "for" variable "I". If I were to say that i >= names.length it shows the list of my names in the array that I created, but it displays an undefined element. There are 5 names in my array, called names, therefore; names.length should be equal to 5. I guess my question is that since the correct syntax is "for (i = 0; i < names.length; i++)" shouldn't that leave me with 4 names instead of 5, because my array has 5 names and since name.length must equal five. In this case, the for loop should stop at the fourth name in the array? Please let me know your thoughts on this. I know I may have over complicated this question and explanation and I do apologize, but I would love for someone to give me an answer if they can understand what my question is.


// Click on "Stuck? Get a hint!" if you get stuck!
var names = ["Llansi", "Tyler", "Steve", "Niki", "Haley"];
for (i = 0; i < names.length; i++) {
    console.log("I know someone called " + names[i]);   
}


#2

You should contact a Codecademy Moderator for this topic. Because i = 0, you cannot have it to be bigger or equal by any reasons, so it should always be i < 0. Making "i" have a bigger value will make an infinite loop. (Don't even try it.)


#3

Well programmers like to start counting from 0 instead of 1 so if you want to index 5 elements you'd go with 0,1,2,3,4 instead of 1,2,3,4,5. This ensures that the length of the array is always 1 bigger than the biggest array index and therefore:

for (i = 0; i < names.length; i++)

let's you traverse any form of array, you just need to change the variable name :slight_smile:
Now if you'd try <= instead you'd go outside of the boundaries, you can't request 6 entries when you only provided 5. So where many programming languages would just throw an error for leaving a fixed data structure, JavaScript just gives you the default value of undefined.

Hope this makes sense.


#4

Array index always starts from 0 and the length of the array is always 1 bigger than the biggest array index therefore
when you write

for (i = 0; i <= names.length; i++)

the code works like-

for (i = 0; i <= 5; i++)

so when i=5 it means names[5]
so it displays an undefined element beacause there is no element.
your question is that since the correct syntax is "for (i = 0; i < names.length; i++)" shouldn't that leave me with 4 names instead of 5.
why should it be?
because if you write

for (i = 0; i < 5; i++)

when i=5 it means i is not less than 5 so the loops stop.
so the last loop is i=4,
it works like-

names[i],names[i],names[i],names[i],names[i],names[i]

means

names[0],names[1],names[2],names[3],names[4]
As array index always starts from 0
therefore for loop shouldn't stop at the fourth name in the array.
you can say that your array is less than or equal to your for variable like this :slight_smile:

var names = ["Llansi", "Tyler", "Steve", "Niki", "Haley"];
for (i = 0; i <= names.length-1; i++) {
console.log("I know someone called " + names[i]);
}

@derekmarkheim
Hope this makes sense.
feel free to ask me any questions :slight_smile:


#5

Thank you very much haxor789 and rcodeman, this makes perfect sense. I should have remember that the zero index rule applies to all situations in dealing with arrays.