27/13


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

This have mede me carzy!! Pleese what is wrong with the code?


#2

You have to show that family is an array. So after this:

]
Write:

var family = [];

It's kind of like what we did in the lesson "Search Text for Your Name", where we did the same thing with the hits.


#3

this reply my computer to me

alice
bob
michelle
timmy


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

I do not understand the text...


#4

So at least it works. Good. tagjumper20201, that's what's supposed to happen, using your code. I suggest that you now refresh.:smiley:


#5

Thanks for all help! :smile:


#6

// 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(var i = 0; i < family.length; i++) {
console.log(family[i]);
}
//result
{ name: 'alice', age: 40 }
{ name: 'bob', age: 42 }
{ name: 'michelle', age: 8 }
{ name: 'timmy', age: 6 }


We're running a test below to make sure your code works.
{ name: 'alice', age: 40 }{ name: 'bob', age: 42 }{ name: 'michelle', age: 8 }{ name: 'timmy', age: 6 }