Building an Address Book: We Made a Friend!


#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];
function add(first, last, EMail, number) {
var newFriend = {
firstName: this.first,
lastName: this.last,
email: this.EMail,
phoneNumber: this.number
};
contacts.push(newFriend);
}
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function list() {
contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}
function search(lastName) {
contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if(this.lastName = lastName) {
console.log(printPerson(bob));
}
}
}
add("Derek", "Sturm", "dereksturm263@gmail.com", "(425) 309-8019");
list();

This is my code ^
Below is what it returns

Bob Jones
Mary Johnson
undefined undefined

It tells me that Oops, try again. It looks like your new contact doesn't have a firstName property.


#2

You are missing huge line of text from your code.


#3

Can you tell me what?


#4

well here is my working code var add = function(firstName,lastName,phoneNumber,email){
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
}

}
add("Ddawg2005","dogs","(999) 889-6789","ddawg2005@gmail.com")
list()


#5

Here you are intermingling defining an object and calling a property for an object. The code should either look like this:

var newFriend = {};
newFriend.firstName = first;
newFriend.lastName = last;
newFriend.email = EMail;
newFriend.phoneNumber = number;

OR your code should look like this:

var newFriend = {
firstName: first,
lastName: last,
email: EMail,
phoneNumber: number;
}