27 Loop the Loop


#1

When I run my code this shows up:
"Oops, try again. It looks like the object at index 0 was not created with the Person constructor"
And I get this error message:

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

TypeError: Cannot read property 'name' of undefined"

This is my code:

// 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 tell me what I need to fix?


#2

HI this part

family[0] = Person("alice", 40);
......

it'ts missing new before Person


#3

thanks that worked :slight_smile:


#4

So I'm getting this error: SyntaxError: Unexpected token )

I'm sure this is some silly missed character somewhere but I just can't see it. Can anyone help?

/ 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(i = 0, i < family.length, i++){
    console.log(family[i].name);
}

#5

HI your statement inside the for loop should be separate with ; instead of ,

for (statement1; statement2; statement3) {

}

#6

Knew it was something silly. Thank you.