Objects I , Loop the loop




We're running a test below to make sure your code works.
TypeError: Cannot read property 'name' of undefined

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++) {


Take a look at your for loop.

It will continue to loop over the family's array as long as one condition is met.
However, we don't want that condition to exceed what we want. In other words, we don't want our i to exceed the family's index.

It's a small tweak but it'll fix it for you!


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

The issue is the <= sign in your iterating condition—it needs to be switched to a < instead.

Remember that arrays start at index 0. That means that, here, family.length is going to evaluate to "4," but the last item in your array is going to have index 3. If your iterating condition is "i <= family.length," the loop will loop through "family[4]"—which is going to be undefined, since there's not an item in your array with index 4.


Thank's a lot! I found it right after made a post.


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