0/3 "Did you create a function called printPerson?" (solved)


#1

Hi,

i stuck on "Building an Address Book" 0/3. https://www.codecademy.com/courses/building-an-address-book/0/3

Script behaves as should and console prints:
Bob Jones
Mary Johnson

But i still get "Did you create a function called printPerson?" error each time i try to submit. Tried to define function as declaration, as expression - script works fine, outputs to console, but submit validation fails.

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];

// printPerson added here
var printPerson = function(person) {
    console.log(contacts[person].firstName + " " + contacts[person].lastName);
};

printPerson(0);
printPerson(1);

#2

While this might make sense, and work, it is not what is expected. The person parameter should be an object, not an integer (index).

var printPerson = function (person) {
    console.log(person.firstName + " " + person.lastName);
};

printPerson(contacts[0]);
printPerson(contacts[1]);

Aside

This version of the function is re-usable in that it is not dedicated to any one reference object. It just needs the object that is passed to it to have a firstName and a lastName property.


#3

Thank You! Now i need to wrap my head around all this, thoroughly. Objects are opening quite a door to different ways of solving same problem.

[edit] Ahh, i just went one lesson back and ding! There was an answer "console.log(contacts[1].phoneNumber);".