14. Building an Address Book, unsure about what my error is


#1


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

This is where I need help. I cannot get it to loop the contacts. It just loops the undefined.


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


#2

The manner in which it behaves differently from what you want usually says a lot about what you'll want to change, so perhaps it would be a good idea to consider that in a more precise way than "loops the undefined". For example, if there's a loop, and it's running forever, then perhaps there is something wrong with the loop condition or something that the condition relies on. Or if it's stopping, but printing the wrong thing, then you'll want to look closer at the thing you're printing.


#3

I had this same problem and it took looking at another post to figure out what was wrong.

What you are doing is trying to console.log() the function printPerson() when in fact you don't need to because the function printPerson() already includes a console log. See below:

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++) {
        printPerson(contacts[i]);
    }
};

list();

#4

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