Listing everybody


#1

Hello! Can anyone help me to understand what is wrong with my code?
I pass the exercise, however it prints something wrong...
"Bob Jones
undefined
Mary Johnson
undefined
Bob Jones
undefined
Mary Johnson
undefined"

the code is:

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();

Listing Everybody
#4

@codingstudios suggestion is not relevant to your problem.

this line:

 for( var i=0; i< contactsLength; i++) { console.log(printPerson(contacts[i]));

you have a console.log() around your printPerson function call, this will cause logging of what the printPerson returns.

Your printPerson function, doesn't return anything, so it returns the default (undefined), once you remove the console.log(), the undefined should be gone


#6

I found the problem,

for( var i=0; i< contactsLength; i++) { console.log(printPerson(contacts[i]));

should read

for (i = 0; i < contacts.length; i++) {printPerson(contacts[i]);

#7

yes, it should. And i also explained why:


#8

HA, yep.. works! Thanks! You guys are great!


#9