27. Loop the loop ""SyntaxError: Unexpected token ["


#1



Keeps throwing "SyntaxError: Unexpected token [" at me and I have no idea what I've done wrong.

If possible I'd also appreciate an explanation of what I've done wrong and why it's not working in addition to a solution, that way I can try to understand what I've done wrong and avoid whatever mistake I've made here in the future.


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

// Now we can make an array of people
var family = [];
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

it needs to be

var family = new Array();

#3

Still getting the same error here.


#4

Oh oops, I lied, the bigger problem is that you're declaring the array indexes as var, but there should be no var keyword, so it would look like:

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)

#5

Wow, now that you mention that should have been obvious even to a beginner like me.

Thank you.


#6

don't sweat it, i didn't see it either, :slight_smile:


#7

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