Loop the loop


#1


Please what's wrong with my code, i get the error below

TypeError: Cannot read property 'name' of undefined


// 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("My name is " + family[i].name);
}


#2

This is attempting to read past the end of the array. There is no element index[4].


#3

Got it! I removed the "=" sign and it worked. Thanks


#4

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