27.loop the loop, TypeError: Cannot read property 'name' of undefined


#1

// 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);
}

TypeError: Cannot read property 'name' of undefined, help please, what am i doing wrong?


#2

Hi here

family[3] = new Person("timmy" 6);

You forgot to put a , between "timmy" and 6


#3

omg hahaha always happen to me thanks!!!!


#4

now its said cannot read property 'name' of undefinde....


#5

Here

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

Remove the = to <=


#6

awww right because we dont have family[4]!!! ty very much


#7

thank you! :slightly_smiling:


#8

You don't need_ "console.log(family[i].name); "_
get rid of .name because it there is no .name in the family array. It should work then.


#9

correct code:

for(var i=0; i console.log(family[i].name)
}


#10

Hi remenber that the for loop should have this strutture

for (statement1, statement2, statement3) {
    #do something 
}