27 Loop the loop


// 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(x = 0; x<=family.length; x++){


I get the message: "Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: undefined is not an object (evaluating 'family[x].name')"

I've tried everything, I dont know whats going on, please help.


This should take into account zero-indexiong, which means we stop before we reach the length. Yes, there a four elements, but if we iterate though 0 to 4 we reach the first non-existent element. Note your index numbers above. 0..3.

x < family.length;


Ohh I would never tought about that...
Thank you!! :slight_smile:


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