3/6 I print out what is required, but still get an error "Oops, try again. Did you create a function called printPerson?"


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

printPerson(0);
printPerson(1);

where am I wrong?
thx!


#2

Hi,
what you did worked for me but codeacademy wanna see your code more like this

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

#3

Thx! This helped. :smile_cat:
But I still consider my code more elegant, as it requires less typing when calling function.


#4

Maybe :smile:
BUT
If you have a second and more array with objects which you wanna print.
It doesnt work anymore.

With the way from codeacedmy it would.


#5

Indeed! Grateful for your explanation! :+1:


#6

explain to me why mine is wrong and yors is right??
my code:
var contacts = [bob, mary];
// printPerson added here.last
var printPerson = function(person){
console.log(bob.firstName + "" + bob.lastName);
console.log(mary.firstName + "" + mary.lastName);

};
printPerson(contacts[0]);

printPerson(contacts[1]);;


#7

Hey!
Your function is not universal.
The goal of the function "printPerson" to print out first and last name of the person you pick.
If you add another person you will have to update your function, with the same efforts you can just print out these names with console.log ...

You also define "person" as a parameter of your function, but you do not use it inside the function.

Following code makes your function universal , you can pick any person by this function and it'll print out first and last names:

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

#8

thanks man,ave understood