# Loop the Loop

#1

This lesson is really hard for me. I am trying to figure this one out and I keep getting it wrong. Any suggestions?

The error message says "make sure to log to the console the name of each object."

``````// 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 (i = 1; i < 5; i++) {
console.log(family.Person);
}``````

#2

I think the problem is in the last 3 lines, but I can't figure out what it is.

#3

``````// loop through our new array
for (i = 1; i < 5; i++) {
console.log(family.Person);
}``````

The right code:

``````// loop through our new array
for (i = 0; i < 4; i++) {
console.log(family[i].name);
}``````

manage to see the diffrence?

It needs to be '0' and '4' in the if statement because as you can see, it start counting from 0:

family[0]

To 4:

family[3]

and yeah, if you want to count till a random number you need to type the number above it. So for example, if you want to count till 5 you type 6. if you want to count till 56 you type 57. got it?

#4

Yes. Thank you so much! I'll check and see if it works.

#5

Yay!! Thank You!!

#6

Could make it easier and not have to put a specific value in the loop:

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

Using family.length will just run through what't in the array, no need to put a specific number. Makes it easier if the array was a few dozen items deep.

#7

That does make it a lot more efficient. I will make sure to do that from now on! thanks!

#8

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