27 Loop the Loop


#1

Appreciate all the help up to this point, but once again...I am stuck.
This is what I have so far, I just get lost in the loop and how to access the correct info in the array, or in the function or in the object....AAAHHH!!!! I don't know, this is what I have. Thanks again.

// 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 name in family){
console.log (name);
}


#2

At least you forgot the {} around the stuff that should be part of your function. And what do you want to see with your loop?


#3

Your for loop is wrong... Remember to use the correct notation :smile:

for (var i=0; i

}

i guess you can figure now what should be inside the loop. Also remember that you need to take elements from the array in order to show them on the console. You can do that by family[].

Hope this helps you.


#4

Thanks for everything so far. Now I'm getting a syntax error: expecting ';'.

// 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=0; i < family.length; i++){

console.log(family.name);

}


#5

Ok, just got this one done.

my last line was

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

Can someone tell me why I need the [i]? I dont follow the reasoning behind it. I just remeber objectName.propertyName. Nothing about including the [i].

Thanks all.


#6

You need to access to the array element. When you do family[i] and i=0 (for the first step inside the for loop) you are calling family[0] and that is an element from the array. You can call whatever property is defined in the constructor of the object then. For example:

familiy[0].name -> accessing to the property name of the first element of the array family which, in this particular case, is the object alice.