27.Loop the loop


#1

Please let me know whats the mistake.

// Our Person constructor
var Person(name,age)
{
this.name = name;
this.age = age;
};

// Now we can make an array of people
var family= new Array ();

// loop through our 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);

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


#2

Got answer had used var instead of function in declaration.


#3

Hey @terasolver21131! You got a couple of problems! First when you ask us questions pls format your code by selecting over your code and hitting CTRL + K. Secondly when you add the Person contructor you didnt initalize it correctly.(not a var but a function)

Your Code:

Correct Code:

function Person(name,age)
{
this.name = name;
this.age = age;
};

Next thing is that you didnt initalize your Array Correctly: You must have a bracket inside it.

Your Code:

Correct Code:

var family= new Array ([]);

Hope it helped! If it didnt please reply saying so!


#6

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
console.log (family.length);
for (var i = 0 ; i < family.length ; i++ ) {
console.log (Person(name));
};

and i am not entirely sure what i am not doing right..
plz help...
Thanks..
Xd


#9

// Our Person constructor
var Person = function(name, age){
this.name = name;
this.age = age;
};
// Now we can make an array of people
//var family = [];
var family = []; // or :: 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[i].name);
}