Loop the loop


#1

hi
i got stuck , but i could find the mistake
thanks

function Person(name,age){
this.name=name;
this.age=age;}
var family=new Array();

// Now we can make an array of people
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(“name is”+family[i].name)}


#2

using <= in the for loop means the loop will also try to access 5th item (at index 4) of the array, which doesn’t exist, which is what throws the error


#3

@stetim94
thank you for your help


#4

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