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
// 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 +”!!”);
}
1 comments
thanks man!
Answer 52f687fd8c1ccc24a50039a1
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]
4 comments
I don’t see any difference between your code and mine… I still have the family[0] family[1] etc
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.
That worked! So hard to catch these mistakes Thank you so much
No problem. Glad it helped :)!
Popular free courses
- Free course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner Friendly4 Lessons - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly11 Lessons - Free course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner Friendly6 Lessons