27. Loop the loop


#1



https://www.codecademy.com/en/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#


Hi what am I doing wrong?


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

// Now we can make an array of people
var family = new Array();
family[0] = {name: "alice", age: 40]};
family[1] = {name: "bob", age: 42};
family[2] = {name: "michelle", age: 8};
family[3] = {name: "timmy", age: 6};

// loop through our new array
for(i = 0; i < 5; i++) {
    Person(i);
};


#2

you have to use your Person constructor to create the four objects and put them in the array.
like-
family[0]=new Person("alice",40);

and think what should be your loop to console the name of the four object.

@nsanjay


#3

Hi I am still unable fix my code.

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

// Now we can make an array of people
var family = new Array();
family[0] = new Person(name:"alice", age:40);
family[1] = new Person(name:"bob", age:42);
family[2] = new Person(name:"michelle", age:8);
family[3] = new Person(name:"timmy", age:6);

// loop through our new array
for(i = 0; i < 5; i++) {
    return family[i].Person;
};

#4

At first remove both "new" from

this.name = new name;  <= remove "new"
this.age = new age;    <= remove "new"

and then
remove "name:" and "age:" from

family[0] = new Person(name:"alice", age:40); <= remove "name:" and "age:"

your code should be like-

family[0]=new Person("alice",40);

because you already created name and age parameter(The code in the parentheses is called a parameter) into Person constructor.so when you are creating new object on Person constructor,first value goes to first parameter and second goes to next one.
like
"alice" goes to name and "40" goes to age
so you don't need to write "name:"
ok?

and again think about for loop :slight_smile:

@nsanjay


#5

Hi I am receiving this error:

Oops, try again. It looks like you have an error in your code. Here's the message: TypeError: Person[40] is not a constructor

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

#6

this is my code and it doesn't help. some assistance please

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

#7

this code prints the name but has the error::
TypeError: Cannot read property 'name' of undefine

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

}


#9

    i < family.length;

We need a conditional as the middle expression.


#12