3/6 Displaying People (Did you create a function called printPerson?)


#1

Hello again good people of the codecademy help forum,

I'm stuck on what I thought was simple. Here's 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 printPerson = function(person) {
person = contacts[person];
console.log(person.firstName + " " + person.lastName);
}
printPerson(0);
printPerson(1);

Others have said this works so I am confused as to what is going on... I thought I created a printPerson function!


#2

The code returns "Bob Jones" and "Mary Johnson" so I don't understand what is wrong.


#3

I had that same problem-
Just take out the

person = contacts[person];

and instead of using the "0" and "1" when calling the function, use "bob" and "mary".


#4

It looks like you've forgotten to create an array for "contacts" with bob and mary in it. Then, you call the function and put the array selection in the parameter:

...

var contacts = [bob, mary];

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

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


#5

function printPerson(contacts) {
console.log(contacts.firstName + " " + contacts.lastName);
};
printPerson(contacts[0]);
printPerson(contacts[1]);
this works


#6

Thanks guys!

alphawhitehat: this is the only one that worked out for me thank you!

Rebeccapaint: this doesn't seem to work, and returns "undefined undefined"

lisadoh: I think there's a mistake slip up with person/contacts ? but yes, I did forget the array with both bob and mary in it!


#7

This is the complete code. It works!

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(person.firstName + " " + person.lastName);
};

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


#8

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 use object pro function in with console.log //
var printPerson = function (person){
console.log(person.firstName + " " +person.lastName);}

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