27. Loop the loop


#1

Why do I get error message "SyntaxError: missing before statement"??

// Our Person constructor
function Person(name,age){
this.name = name;
this.age = age;
};
var family = new Array();
// Now we can make an array of people
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);
// loop through our new array
for(var i=0; i < family.length; i++){
console.log(family[i].name);
}


#2

Spotted the Problem

This is how you create a Array: var family = new Array([]);
Not like your way: var family = new Array();

If you would like code:

    // Our Person constructor

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

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);

}

// loop through our new array

#4

I see! it still doesn't work though. I'm getting same error message


#5

What does it say on the error message?


#6

Edit: Sorry if you copied my code i made a mistake on the third Array

family[2] = new Person('michelle', 8);

Change it to :

family[2] = new Person("michelle", 8);