Social Icons

Pages

Thursday, December 1, 2016

Introduction to Objects I 27/33

Loop the loop
Arrays filled with objects will work just like arrays filled with numbers and strings.
In the same way we may loop through an array of numbers to print them out or calculate a sum, we can loop through an array of objects and access properties or methods.


Instructions
  1. Write a person constructor called Personthat has two properties (name and age).
  2. Create an empty array called family.
  3. 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
  4. Create a for-loop that loops through thefamily array and prints out the nameproperty for each family member in order of creation.
?
Hint
If you need a quick brush-up on how for-loops work, review the exercises on loops.
Accessing the name property in the array and then using this in the for-loop is the trickiest part. Remember that we use dot notation to get the value of a property from an object:
objectName.propertyName;
And we can use the same dot notation to get the value of a property for one particular object in an array.
arrayName[2].propertyName;
This will get the third object in the array, and then find the value associated with the property.
Note that the objects must be in the order specified in the instructions!
                                                                       // 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);
                                                                      // add the last family member, "timmy", who is 6 years old

                                                                     // Our Person constructor
                                                                        for(i=0;i<4;i++){console.log(family[i])};

                                                                     // loop through our new array






No comments:

Post a Comment