4. Listing Everybody


#1

HelloWorld!
I post this topic because I don't understand the result of my 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);
}

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

list();

It prints out :

Bob Jones
undefined
Mary Johnson
undefined

what does the undefined mean ? And how to remove it ?


#2

Hi this line

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

just remove the console.log() around printPerson(contacts[i])


#3

I've just seen it right now! Thank you mate, it runs correctly. :grinning:


#4

In addition to that you can also simplify for loop condition:
for(var i = 0; i < contactsLength; i++) {

of course both ways work well.


#5

Sure. But in the instructions it request to use one less than contacts.length so... I've used it to be rigorous :grin: Thanks too!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.