Loop the loop


#1

Preformatted text


https://www.codecademy.com/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#Oops, try again. It looks like the object at index 0 was not created with the Person constructor.


We're running a test below to make sure your code works.

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] = 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);
}

can anyone help? i dont know how on earth i can define family[i] !


#2

Remember to use the new operator when declaring an instance.

... = new Person( ... );

#3

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