I'm trying to keep up with the instructions described here:Building an Address Book
& that's the result:
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( contacts[person].firstName + " " + contacts[person].lastName )
};
printPerson(0);
printPerson(1);
Is there something wrong with my function or the input argument? Thanks.
Unless you have some question which you failed to write, there is nothing wrong with your function or arguments and it's working as it should!
Code quality can be the point of discussion here, as instead of doing contacts = [bob, mary];
you can use
contacts.push(bob);
contacts.push(mary);
So when you need to add another contact it'll be easier this way, what you have done is more static. Also to print all the persons, you can use the loop inside printPerson
instead of calling it twice:
function printPerson(){
for(var i=0; i<contacts.length; i++) {
console.log(contacts[i].firstName + " " + contacts[i].lastName);
}
In order to pass the test, you will have to do this according to their instructions:
function printPerson(person){
console.log( person.firstName + " " + person.lastName );
}
printPerson(contacts[0]);
printPerson(contacts[1]);
They asked you to pass the person object in the function whereas you were passing the index.