Loop The Loop 27 (Introduction to Objects I)


#1


I have looked at other loop the loop help requests here, and did not see the same issue. It is printing out the names but when i run this in the console i get the same error. With the name is undefined.. So not only am i wondering why this is printing out in the first place. But what is the error exactly.

This is the error message i get.

Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: Cannot read property 'name' of undefined

Console Error.

VM983:18 Uncaught TypeError: Cannot read property 'name' of undefined(…)


// 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(i = 0; i <= family.length ; i++)
{
    console.log(family[i].name);
};

This is the output i get.

alice
bob
michelle
timmy

---
We're running a test below to make sure your code works.
alicebobmichelletimmy
TypeError: Cannot read property 'name' of undefined

#2

here:

i <= family.length

this should be < since arrays are zero indexed based, there is no index 4. so you don't want less or equal to the length (which is 4)


#3

ahh, i see. Thank you for your help.


#4

can we use any other property other than .length?
its illogical ..but can we?


#5

why is length illogic?


#6

I meant can we use any other property? :smiley:

stetim94 Codecademy Moderator
November 27

why is length illogic?


Visit Topic or reply to this email to respond.


In Reply To
webjumper60462
November 27
can we use any other property other than .length? its illogical ..but can
we?


#7

well as you can read here, this is possible, but far more difficult, length is actually a good way


#8

Got it . I'll just learn everything until then :slight_smile: . thank you for that link :smiley:


#9