Introduction to Objects I 27. Loop the Loop


#1


https://www.codecademy.com/en/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


// Our Person constructor
function Person (name,age){
    this.name = name;
    this.age = age;
}

// Now we can make an array of people
var Family = [];

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( var i = 1; i < 4; i++){
    console.log(Family[i].name);
}


I can't figure out what is wrong with this code. Anybody have any clue? it prints all the names out on the console just fine.


#2

Hello, you should start your loop from i=0, in order to get Family[0] element. Also you may have to change Family=>family, because that is what the exercise is asking. Sometimes this can be a real headache.:slight_smile:


#3

Yeah i would agree there but weirdly that is not enough to pass the question. To pass I had to change the Family to family as that's what the question asks.

The finished code should look like this.

// Our Person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
}

// Now we can make an array of people
var family = [];
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 (var i = 0; i < family.length;  i++) {
    console.log(family[i].name)
    }

#4

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