27/33 Help please solve the problem


#1

The task:
Write a person constructor called Person that has two properties (name and age).
Create an empty array called family.
There will be four objects in the array. Using your Person constructor, create the four objects and put them in the array. The order of the objects are:

"alice" who is 40
"bob" who is 42
"michelle" who is 8
"timmy" who is 6
Create a for-loop that loops through the family array and prints out the name property for each family member in order of creation.

// Our Person constructor
var Person = function (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 (i=0; i console.log (i);
}


#2

After filling the family array
do a
console.log( family );
to get a better idear of how the Data looks like.

Your FOR-loop definition is incomplete/invalid.
Read
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array


#3

Sorry, help if you have a minute, I can't understand, how to print only "name" ? What to do with code?:
for (i=0; i console.log (family.name);
}


#4

If you would have defined the family Array
like

 var family=[];
 family[0] = 1;
 family[1] = 2;
 family[2] = 3;
 family[3] = 4;

you would have an Array with 4 elements
( please take notice that the array is starting its internal-count with =zero= )

[ 1, 2, 3, 4 ]

Now in your code you are not assigning number Values
but you are assigning object Values to the Array-elements
which results in an Array

[ { name: 'alice', age: 40 },
  { name: 'bob', age: 42 },
  { name: 'michelle', age: 8 },
  { name: 'timmy', age: 6 } ]

now if you would do
console.log( family[0] ); ==>output>> { name: 'alice', age: 40 }
console.log( family[0].name ); ==>output>> alice
console.log( family[3].age ); ==>output>> 6

Now a nice thing of the Array =prototype=
is it has a so-called length property
and its associated number Value will give you the first-free-element-number
in our case
family.length would be 4.
(remember Array-internal-count starts at =zero= )

With the FOR-loop you have a facility,
with which you can run-over all elements of an Array
The syntax of the FOR loop

for ([initialization]; [condition]; [final-expression]) {
       //FOR code-block
   statement
   }

As we want to get at every element we are going to start at =zero=
[initialization]>> var i = 0;
We want to run as long as there are elements
[condition]>> i < family.length;
We will have to increment i with 1
[final-expression] i = i + 1
(take notice NO semi-colon-; )

Thus we would have

 for (var i=0; i < family.length ; i = i + 1 ) {
       //FOR code-block
       console.log( "Family-member          : " + family[i] );
       console.log( "Family-member-name: " + family[i].name );
       console.log( "Family-member-age   : " + family[i].age );
  }