Skip to content

Instantly share code, notes, and snippets.

@Jared-Prime
Created April 12, 2012 22:09
Show Gist options
  • Save Jared-Prime/2371377 to your computer and use it in GitHub Desktop.
Save Jared-Prime/2371377 to your computer and use it in GitHub Desktop.
Address Book - using objects
// Codeacademy.com project, "Address Book"
// the functions input and output information about objects collected in the array.
// Note that there's no class; you have to define the attributes explicitly when creating the object.
var contacts = [];
function printPerson (person) {
console.log(person.firstName + " " + person.lastName);
}
function list() {
var items = contacts.length;
for (i=0;i<items;i++) {
printPerson(contacts[i]);
}
}
function add(firstName,lastName,email,telephone){
var friend = {
firstName: firstName,
lastName: lastName,
phoneNumber: telephone,
email: email
};
contacts[contacts.length] = friend;
}
function search(lastName) {
var items = contacts.length;
for(i=0;i<items;i++) {
if(lastName===contacts[i].lastName){
printPerson(contacts[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment