Mystery of the undefined family[i]


#1



https://www.codecademy.com/en/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#


So I am stuck as to why this doesn't work/why I don't complete the page. The output below shows the log and that the for loop does its job and prints out .name for each entry to the family array. So why is family[i] undefined? I thought it might be the fact that the objects stored in the array are not assigned their own id's i.e. var person1 = new Person("Alice",42) etc then creating the array as var family=[person1,person2...etc] but this doesn't seem to work either.

Hints, tips, tricks, or the bit I misunderstood anyone please???

Thanks!

Log output as follows: -

alice
bob
michelle
timmy


We're running a test below to make sure your code works.
alicebobmichelletimmy
TypeError: family[i] is 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 (i=0;i<=family.length;i++){
    console.log(family[i].name);
};


#2

@gregb1609,
With your

you will go out of range of the array....

Change your FOR-loop condition
into

i<family.length;

AND then use

for (i=0;i<family.length;i++){
    console.log(family[i].name);
};
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

#3

That's the puppy! On reflection as .length count 1 equals array space[0] the equals isn't needed...

Thanks muchly,

Greg