27 Loop the loop


#1


// 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++){
   console.log((x+1)+"."+family[x].name); 
}


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

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.


#2

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;

#3

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


#4

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