Loop the loop


#1


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


It says: TypeError: undefined is not an object (evaluating 'family[i].name'), and also: Oops, try again. It looks like the object at index 0 was not created with the Person constructor


// 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] = Person("alice",40);
family[1] = Person("bob", 42);
family[2] = Person("michelle",8);
family[3] = Person("timmy",6);

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


#2

you can loop through the array using a number equivalent of the items like this,

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

#3

Can't see why I should use a defined number, when I'am looping through an array? :slight_smile:
anyways.. Found the error.

I didn't define the Person as a new Object.

So it should be:

:sunglasses:


#4

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