6/6 Building an Address Book


#1

What's wrong with my code here? It brings up this error message: Oops, try again. It looks like there are still only two items in the contacts array.

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

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

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

}

};

search("Jones");

var add = function(firstName,lastName,email,phoneNumber) {
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
email: email,
phoneNumber: phoneNumber
};
};

contacts.add = ("Bill","McSample","bill.mcsample@example.com","(213) 555-5555");

list();


#2

I'm not sure why but it works if you do this instead...

add ("Bill","McSample","bill.mcsample@example.com","(213) 555-5555");