Address Book Code

That last bit of the code was tough to understand.

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

/*Create a search function
then call it passing “Jones”*/
var search = function (lastName) {
var contactsLength2 = contacts.length;
for (i=0; i<contactsLength2; i++) {
if (lastName === contacts[i].lastName) {
printPerson(contacts[i]);
}
}
};

search(“Jones”);

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

add(“Dirk”, “Nowitzki”, “Dirk@Nowitzki.com”, “(123) 234-1231”);

list();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s