Get stuck


SyntaxError: Unexpected token )
Oops, try again. There was a problem with your syntax.

// Our Person constructor
function Person (name, age) { = name;
    this.age = age;
// Now we can make an array of people
var family = new Array();
family[0].Person("alice", 40);
family[1].Person("bob", 42);
family[2].Person("michelle", 8);
family[3].Person("timmy", 6);
// loop through our new array
for (i = 0, i < 3, i++) {


As you probably know we define for loop by passing three expressions:

  1. expression that will be executed at the beginning;
  2. condition that will be checked before starting next iteration;
  3. expression that will be executed after every iteration.

You have used commas to separate these parts. Because of that, the interpreter is treating i = 0, i < 3, i++ as a single complex expression and is still waiting for two next parts. You can find more about the comma operator here -> MDN: Comma operator.

To fix this problem you just have to change commas in the for loop to semicolons.


still not working with ";"


That's true, but now you should see a different error - TypeError: Cannot read property 'Person' of undefined.

So let me ask you, is this how we create new Person objects:

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


Just try to find the answer to this one by yourself. You got stuck with the syntax error. I get it and that is why I helped you. But at least try to find the solution to this one.


Codecademy exercise reference

According to the codecademy reference: You can also think of objects as combinations of key-value pairs (like arrays), only their keys don't have to be numbers like 0, 1, or 2: they can be strings and variables.

You wrote an array key

It should only have numbers for array (I think that's a difference between array and object) like: family[0] that's it. Then you can put objects in the array's elements. Like, family[0]= newPerson(bla bla...);


thx i found some solutions but still can't understand how is this working?

// Our Person constructor
function Person (name, age) { = 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 (i = 0; i < 3; i++) {

I can't understand how is taking the last name this way .. instead it's easier to change condition by "i<4" and delete the last console.log function or whatever is it.


I mean for cycle is going with i to 2 then it stops. It exit for loop and then how is incrementing to 3 just from console.log(family[i].name);
That's kind of weird?


You are right, it's better to change the condition to i < 4 or to i < family.length (best solution).

Why this version works, let's see what happens in your for loop.

At the beginning we have to evaluate this expression: i = 0. We omitted var keyword so i is a global variable that stores value 0.

First iteration

0 < 3 ==> True
i++ // now i stores value 1

Second iteration

1 < 3 ==> True
i++ // now i stores value 2

Third iteration

2 < 3 ==> True
i++ // now i stores value 3

Fourth iteration

3 < 3 ==> False ==> end of the for loop

So now, after the for loop value stored in the variable i is 3 :slight_smile: