3.Displaying people


#1

Getting Oops, try again. It looks like your function didn't log "Bob Jones" to the console. Did you remember to call your function on bob?

Code is as 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];

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


#2

Same object, but try using the bob variable instead for the logging.


#3

I changed the logging to:

console.log(printPerson(bob));
console.log(printPerson(mary));

I did get the answer correct, but I am not totally convinced!

Below is the output I received;

Bob Jones
undefined
Mary Johnson
undefined


#4

printPerson doesn't have any return value, so perhaps you shouldn't print its result.


#5

whats up! You have to give your function a name. So try starting your function with var printperson =....

go from there. Hope this helps!


#6

This is my code. It passed and I am getting a double Undefined. Not sure why?
`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 = [0];
mary = [1];
// printPerson added here
var printPerson = function(person) {
console.log(person.firstName +" "+ person.lastName);
};

console.log(printPerson[0]);
console.log(printPerson[1]);

`


#7

@kisabel86, because function printPerson expects bob and mary as arguments. But you don't pass any arguments because it wrong syntax. try this:

console.log(printPerson(bob));
console.log(printPerson(mary));

You created array contacts to store objects bob and mary. But you used wrong syntax to put created objects in array. Should be:

contacts[0] = bob;
contacts[1] = mary;

It means that you put in array object bob by index 0. The same with object mary, but you put by index 1.

More info:
1. w3schools.com - JavaScript Arrays
2. w3schools.com - JavaScript Functions


#8

Solution:
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
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
};

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

#9

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


#10

eroor at this

edit it

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

is ok.


#11

Here is the part of the code that confuses many people.

// contacts array
var contacts = [bob, mary];

/* printPerson function added here, this will print out the first and last name of your contacts */
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
};

// calls the printPerson function
printPerson(contacts[0]);
printPerson(contacts[1]);

I will point out one thing though, by now you must have been taught something very similar to this a few times already. If you don't understand something it is perfectly fine to go back a little and try to refresh your skills. Don't just try to rush through it all, no one can learn all about coding in a day. It takes time.


#13