Skip to content

Instantly share code, notes, and snippets.

@albertcoder
Last active August 29, 2015 14:18
Show Gist options
  • Save albertcoder/dad97d6aa66b104359b8 to your computer and use it in GitHub Desktop.
Save albertcoder/dad97d6aa66b104359b8 to your computer and use it in GitHub Desktop.
Code for Address Book in JavaScript
var alley = {
firstName: "Albert",
lastName: "Coder",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};
var sukh = {
firstName: "Sukhi",
lastName: "D",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};
var contacts = [alley, sukh];
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]);
}
}
function add(firstName, lastName, email, phoneNumber) {
contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
}
/*Create a search function
then call it passing "Jones"*/
var search = function(lastName) {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
if(contacts[i].lastName == lastName) {
console.log(printPerson(contacts[i]));
break;
}
}
}
search("D");
add("Akshay", "Kumar", "ac@csiom.com", "9023232373");
list();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment