27 loop the loop


#1

Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: family[per] is undefined


// Our Person constructor
 function  Person(name, age) {
  this.name = name;
  this.age  = age;
 };
 // Now we can make an array of people
 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(per = 0; per<=family.length; per++) {
    console.log("Hello, my name is " + family[per].name + "!");
};


#2

try changing all of the per's into person.


#3

thaks, I resolve just printing in console

family[per]


#4

// Our Person constructor
function Person(name, age) {
this.name = name;
this.age = age;
};
// Now we can make an array of people
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(per = 0; per<=family.length; per++) {
console.log("Hello, my name is " + family[per] + "!");
};


#5

Hi bitplayer11006,

I found some syntax errors.

// Our Person constructor
 function  Person(name, age) {
  this.name = name;
  this.age  = age;
 };
  ^ //remove this semicolon
 // Now we can make an array of people
 family = new Array(); //should be  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(per = 0; per<=family.length; per++) {
     ^//should be for(var per = 0; per<family.length; per++) 
    console.log("Hello, my name is " + family[per].name + "!");
};
 ^ // remove this semicolon

#6

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