Building an Address Book


#1

What's wrong with code!!

var contacts = [bob, mary];

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

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}

/*Create a search function
then call it passing "Jones"*/

/*var search = function(Bob, Jones) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
if(contacts[i].lastName === lastName) {
printPerson(contacts[i]);
}
}
}
search('Jones');*/
var add = function(firstName, lastName, phoneNumber, email) {
contacts[contacts.length] = {
firstName: "John",
lastName: "Taylor",
phoneNumber: "(508) 444 - 4444",
email: "taylorjohn@example.com"
};
};

add("John", "Taylor","(508) 444-4444","taylorjohn@example.com");
list();


#2

It looks like you've turned your search function into a comment. Also, its parameter (the thing in the parentheses) should be lastName, and not Bob, Jones. It also looks like you have included printPerson twice in the function.


#3

Thank you very much!, I was pulling my hair out on that one


#4

Here is my simple code and working fine..

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];
console.log(mary.phoneNumber);