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 = [];

family[1].new Person = ("alice", 40);
family[2].new Person = ("bob", 42);
family[3].new Person = ("michelle", 8);
family[4].new Person = ("timmy", 6);

// loop through our new array
for(i=0; i<family.length; i++) {
console.log(family[i].name);
}

I dont understand task. help me
what is wrong??


#2

HI these lines

family[1].new Person = ("alice", 40);
family[2].new Person = ("bob", 42);
family[3].new Person = ("michelle", 8);
family[4].new Person = ("timmy", 6);

if you wan to create a new person with teh Person constructor you should do it like that

/*I create john*/
var john = new Person("John",1000);

in your case you don't need of var and where I put john you will put family[]


#3

Hi
I had the same problem.
You should create those object with the Person constructor before you adding them into the family array.
BTW you should start putting the object into the array with family[0] as array indexes start with 0.


#4

Thanks for the answer, but I do not quite understand how should look like my code

So?

var new Person = (family["alice", 40]);
var new Person = (family["bob", 42]);
var new Person = (family["michelle", 8]);
var new Person = (family["timmy", 6]);

Write SyntaxError: missing variable name


#5

No It should look like that

Family[0] = new Person(...);
...

#6

Wow! Code is right! Thank for help! :slight_smile:


#7

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