Building an Adreess BOOK


#1

WHATS THE MISTAKE I AM DOING HERE...

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

#2

for(var i=0; i<contactsLength.length;i++)

contactsLength contains the length of contacts, so i don't need the length of contactsLength.

console.log(printPerson(contactsLength[i]));

contactsLength is the length of the array, you need to access the array, not the length of the array


#3

i HAVE THE CORRECTED THE CODE AND GETTING THE OUTPUT LIKE
Bob Jones
undefined
Mary Johnson
undefined
Bob Jones
undefined
Mary Johnson
undefined

I am wrong somewhere. Can you help me with this.

------------------------------ code-----------------------------------------

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(var i=0; i<contactsLength;i++)
    {
        console.log(printPerson(contacts[i]));
    }
};
list();

#4

this is a mixture of the exercise calling your function for validation purposes (which you can't do anything about) and this line:

console.log(printPerson(contacts[i]));

what is the purpose of printPerson? to log information to the console. So you don't need to use console.log() on the the line i posted above. This will log what the function returns (which is undefined by default) to the console

i put your code here, press the run button in the top right, and everything will work fine


#5

Thnks for ur help :slight_smile:


#6