Profile image of JChauPlayzMC
Submitted by JChauPlayzMC
over 11 years

27/33 Loop the loop error TypeError: 'undefined' is not a constructor HELP

I can’t get my code to work. Here is my code:

// 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("My name is " + family[i].name + ", and I am " + family[i].age + " years old.");
};

I keep getting the error “TypeError: ‘undefined’ is not a constructor.” I have tried changing the family[i].name and family[i].age in the console.log to Person[i].name and Person[i].age, but that does not work as well. Does anyone have any ideas as to why my code will not work, and the correct code required to go on? Please help!

Answer 538da7e180ff33122a001d99

2 votes

Permalink

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

// Now we can make an array of people var family =[]; 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( “My name is” +family[i].name +”& my Age is”+family[i].age +”!!”);

}
Profile image of rahuljaincse
Submitted by rahuljaincse
about 11 years

1 comments

Profile image of bharat2015
Submitted by bharat2015
over 10 years

thanks man!

Answer 52f687fd8c1ccc24a50039a1

0 votes

Permalink

Cleaned it up for you.

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



// Now we can make an array of people
var family = [];
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("My name is " + family[i].name + ", and I am " + family[i].age + " years old.");
}

You started your family here at [1] - not at [0] - so when it tried to get you the contents at family[0] it would come up empty and give you an error. Remember we always start at position [0] ->

var family = [];
family[0] = ;  // here you had family[1]
family[1] = ; // here you had family[2]
family[2] = ; // here you had family[3]
family[3] = ; // here you had family[4]
Profile image of lloan
Submitted by lloan
over 11 years

4 comments

Profile image of JChauPlayzMC
Submitted by JChauPlayzMC
over 11 years

I don’t see any difference between your code and mine… I still have the family[0] family[1] etc

Profile image of lloan
Submitted by lloan
over 11 years

That’s weird - When I copied it - I remember having the first instance where you added to the family array @ [1] - In any case, there are little differences like how I made the array - literal notation - and how I made the new person. In any case - the issue here is the new person area - Use parentheses.

Profile image of JChauPlayzMC
Submitted by JChauPlayzMC
over 11 years

That worked! So hard to catch these mistakes Thank you so much

Profile image of lloan
Submitted by lloan
over 11 years

No problem. Glad it helped :)!