SyntaxError: Unexpected token =


#1


https://www.codecademy.com/en/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661


I keep on obtaining: "Oops, try again. There was a problem with your syntax." Could someone tell me what's wrong with it?


function = Person(name, age)
{
    this.name = name;
    this.name = 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);

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


#2

Hi,

First thing: look at your first line "function = Person(name, age)". Remove "="!
Second Thing: look at your 3 and 4 lines: 1 - this.name = name; is correct and 2 - this.name = age; is not correct (change here this.name to this.age)

Then your code will run! :slight_smile:

Here is correct code for example:

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


#3

I'm such an idiot. I don't know why I put down this.name instead of this.age. It works now. Thank you!


#4

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