Section 27 Loop For Loops How to do the For Loop?


#1

Just need some help structuring a for loop for this array lesson :smiley:


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


#2

Stuck on the same thing. I tried

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

Not sure if should include a method somewhere in Person function which can then call here??


#3

Ok now I've got it to work as realised I was an idiot who used commas instead of ; in brackets. So that works :stuck_out_tongue:


#4

mind putting what worked in here?


#5

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


#6

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