27 - Loop the loop


#1

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

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

for (var i = 0; i < family.length; i++) {
console.log(family[i].name);
}

Whats wrong with this, it keeps telling me unexpected token [ but i just cant find it!


#2

In the objects array, you don't have to put the "var" before family[x] = new Person...

It should be like this:

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


#3

And you need to write new Array with capital A so the program can recognize that you are talking about Array and not some variable called array


#4

That still wouldn't work, dayfiri. Array would need to be a function as well. It should be "new Array();", otherwise there would be an error.


#5

Where you have mentioned "var family=new Array"--> instead use " new Array();"


#6

Can anyone explain to me why I couldn't use a For / In loop to get the same result. My loop was

for (Person in family) {
console.log(family.name);
}