27. Loop the Loop Unexpected Identifier


#1

Hello, I'm stuck on number 27.
I have no idea what's wrong, but I keep getting "SyntaxErrror: Unexpected identifier"

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

Any help is appreciated. Thank you!


#2

Your constructor should be:

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

Next problem is here:

var Family = new Array(); // you used dot after new keyword and missed ;
// actually, variables should begin with lower case
// also new Array() is a bad practice, use [] instead:
var family = [];

Next part of code is fine

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

but you can to use push() method which add element at the end of array

Family.push(new Person("alice",40));

Now when you have an array with elements you can print it. But here you also have syntax errors:

for (var i=0; i<Family.length; i++) { you don't need ; after i++
    console.log(family[i].name); // family is not declared, use Family instead
};

#3

Thank you for such a detailed reply! That fixed it. It seems that I should probably look out for those syntax errors involving semicolons vs periods. But thanks again for your help.


#4

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