Loop the loop


#1

Hi. I have no idea why this is wrong.
the console prints out the names then tells me there's an error.

function Person (name, age){
    this.name = name;
    this.age = age;
}
// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice",40);
family[1] = new Person("bob",42);
family[2] = new Person("michelle",8);
family[3] = new Person("timmy",6);

// loop through our new array
for(i = 0; i <= family.length; i++){
    console.log(family[i].name);
}

#2

the length of the array is 4, but arrays are zero indexed based, so the indexes are: 0, 1, 2 and 3

so you want values lesser then the family length, not lesser or equal to the family length


#3

yea, it would be i<family.length in the for loop. Also the console.log(family[i]); the .name will mess it up.


#4

should be

for(i = 0; i < family.length; i++ {


#5

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