Loop the loop


#1

Hello guys. I have one question:
Is my code bad and why ? it should work but the program writes an error..

// Our Person constructor
function Person (name, age) {
    this.name = name;
    this.age = age;
};
// Now we can make an array of people
var family = [];
while (family.length != 4) {
    family.push(new Person (prompt("Name"), prompt("Age")));
};
// loop through our new array
for (var x in family) {
    console.log(family[x].name);
};

#2
family.push(new Person (prompt("Name"), prompt("Age")));

A database breach just waiting to happen. An array push is tantamount to pushing data to a database, and this presents no validation parameters, whatever. A dangerous and precarious way to program, even if it does work.


#3

var family = new Person// Now we can make an array of people
var family = [];
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(var i = 0; i < family.length; i++);
{
console.log(family[i].name);
}// loop through our new array
WHAT IS WRONG? IT KEEPS SAYING LOG EACH NAME ON CONSOLE...DID I MAKE FAMILY AN ARRAY


#4

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

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);
// Now we can make an array of people

// loop through our new array
for (var i=0; i console.log(family[i].name);
};


#5

This line can go. Do you have a Person constructor written above your code? If not, be sure to write it, then follow with what you have:

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

Be sure you do not include a semi-colon in a for statement, such as you have done above. This is incorrect...

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