Introduction to Objects I: Loop the loop


#1

This is my code:

// 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("My name is " + family[i].name);
};

I don't know what I am doing wrong please help


#2

Hi @homerpizza

Your for loop should be looks like :

for(var i = 0; i < family.length; i++)

Hold on :wink:


#3

Thank you so much! I have been stuck on this for ages


#4

But why? Why should it be < and not <=? Wouldn't < not print the last family member? And also, @homerpizza, you should have semicolons and not commas in your for loop.


#5

@jrpsychgal if you write this for loop

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

It's going to display 0,1,2,3,4 (5 numbers)

family has 4 variable, not 5
So we write < to have 4 variables who will return