My code is messed up


#1

What is wrong with my code? The error that comes up is "Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: Cannot read property 'name' of undefined"

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


#2

@parthmohanty

Can we have the link for the exercise?


#3

// loop through our new array
for(var i = 0; i < family.length; i++) {
    console.log(family[i].name);      // prints out the name property for each family member 
}

just delete "My name is" + in your code


#4

It still shows the same exact error


#5

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


#6

@parthmohanty

<= should be < because we are printing the whole array.


#8

Thank you so much. It worked