Displaying People


#1

`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 = function(bob);
console.log(contacts[1]);
}`

SyntaxError: Unexpected token


#2

Those lines do not belong inside the function body. All you need to do is call the function from outside:

printPerson(contacts[1]);

#3

`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]);
}`
Does not work


#4

That's because the two calls are still inside the function body. They should be below the }

};
printPerson();

#5

Hi,

My name is Ferran and I encounter an error in Displaying People although I get the wanted output. I hope you can help me sorting out what is wrong:

The error message is the following:

Oops, try again.
Did you create a function called printPerson?

The output I get is:
Bob Jones
Mary Johnson

The code I implemented is:

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

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

Thanks!
Ferran


#6

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

you have to call on the function from the outside of it.


#7

var printPerson = function(person) {
console.log(person.firstName + " " + person.lastName); //So for example if it was bob you would read it now as (bob.firstName + " " + bob.firstName)
};
printPerson(contacts[0]) //in this bit you grab bob from the array and it is placed where it essentially replaces person in the function.
printPerson(contacts[1]) //in this bit you grab mary from the array and it is placed where it essentially person in the function.

Remember you need to use the [] not the () when dealing with arrays.
Also next time you should make your own topic instead hijacking someone else's or see if someone else with your same problem has a solution.


#8

OK many thanks and sorry for the hijacking. It was my first question and I actually didn't know how the forum works. Next time I will make my own topic as you say.