Building an Address Book issue


#1

https://www.codecademy.com/courses/building-an-address-book/0/4?curriculum_id=506324b3a7dffd00020bf661#

The code works but it console.logs out undefined as well. When i had the (var i =0; i<contactsLength; i++) as (var i =0; i<contactsLength -1; i++) it wouldn't work but once I took out the -1 it worked. I want to make sure I understand whats the issue not just complete the challenges. Can anyone help me?


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


#2

The function already logs, but has no return value. Since you are logging again, you are logging undefined.

console.log(console.log());    // undefined

#3

I changed console.log(printPerson(contacts[i])) to return (printPerson(contacts[i])) but it won't log mary johnsons name


#4

There is no need to return. Just don't log the function call expression.

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

#5

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