Loop the loop


#1

hey guys,

can you see where i am going wrong in this code, i have looked at others and i jus cant see it.

// Our Person constructor
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(var i = 0; i <= family.length; i++){
console.log(family[i].name);
}


#2

Problem is here:

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

So, in the last iteration i will be equal to family.length. We start indexing elements in arrays from 0. So if we have array ["a", "b", "c"] element at index 3 (length of this array) does not exist, right?

You have to change condition to:

i < family.length