Listing Everybody


#1

what purpose does the variable contactsLength serve? the function works fine without it.

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

var list = function () {
    var contactsLength = contacts.length;
    for ( i = 0; i < contacts.length ; i++) {
        printPerson(contacts[i]);
    }
};
list();

#2

Well as it has the value of contacts.length in it, so you could use it in the for loop instead of contacts.length, but as it is almost the same size it really doesn't make that much sense.


#4
function list() {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        printPerson(contacts[i]);
    };
};

The reason they ask us to define the variable is they want us to use it in the for loop instead of the .length method. We just use contactsLength instead.