4. listing everyone


#1

Hi all,
can anyone explain why my code prints the names in the list but doesn't pass the lesson.

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 contactsLength = function list(){
for(i=0; i printPerson(contacts[i]);
}

};
contactsLength();


#2

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

Dont just copy it look what you did wrong and learn from it.


#3

Hi,
For some reason parts of my list function code is not being shown correctly.

I now understand why I kept getting an undefined message when I tried calling list().

Thanks for the reply.


#4

@mrbumbum In your for loop shouldn't you be using contactsLength since that variable is set to contacts.length?


#5

Hi, Your problem is, it asks you to set variable to contact.length...you have set it to function list....In your for loop (i) needs to be less than the variable contactLength or contacts.length, then increase i by one each time (i++)... Then call the printPerson function and pass in the contacts[i]........Finally call the list function......I find this difficult to grasp but i am slowing getting there.....

var contacts = [bob, mary];
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
};
function list()
{
var contactsLength = contacts.length;
for (i = 0; i < contacts.length; i++)
{
printPerson(contacts[i]);
}
}

list();