Problem in 27. Loop the loop


#1

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

The code below isn't accepted for some reason, although I see the names printed out as they should be in the console.


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

I altered line 16 after that by changing

console.log(family[i].name);

to

console.log(family[i]);

The code then gets accepted even though the output which I receive doesn't seem to be what is actually required (just the name is what is required)

Is this just a bug, or am I doing something wrong?


#2

Here

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

It should be i <family.length not i<=family.length

because length is always greater-than index

there is no value for family[4].name


#3

Thanks Mr. rcodeman, that solved the issue!

For some reason, it still accepts this:

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

#4

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