Advertisement
gnamp

Objects 1, 6: Solved

Feb 10th, 2012
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var bob = {
  2.     firstName: "Bob",
  3.     lastName: "Jones",
  4.     phoneNumber: "(650) 777 - 7777",
  5.     email: "bob.jones@example.com"
  6. };
  7.  
  8. var mary = {
  9.     firstName: "Mary",
  10.     lastName: "Johnson",
  11.     phoneNumber: "(650) 888 - 8888",
  12.     email: "mary.johnson@example.com"
  13. };
  14.  
  15. var contacts = [bob, mary];
  16.  
  17. function printPerson (person) {
  18.     console.log(person.firstName + " " + person.lastName);
  19. }
  20.  
  21. function list (){
  22.     numberOfItems = contacts.length;
  23.     for (i=0; i<numberOfItems; i++){
  24.         printPerson(contacts[i]);
  25.     }
  26. }
  27.  
  28. function search(lastName){
  29.     numberOfContacts = contacts.length;
  30.     for (i=0; i<numberOfContacts; i++){
  31.         if(lastName === contacts[i].lastName){
  32.         return printPerson(contacts[i]);
  33.         }
  34.     }
  35. }
  36.  
  37. function add(firstName, lastName, email, telephone){
  38.     var newContact = new Object();
  39.     newContact.firstName = firstName;
  40.     newContact.lastName = lastName;
  41.     newContact.email = email;
  42.     newContact.telephone = telephone;
  43.    
  44. contacts[contacts.length] = newContact;
  45. }
  46. var firstName = prompt("First Name?");
  47. var lastName = prompt("Last Name?");
  48. var email = prompt("Email?");
  49. var telephone = prompt("Telephone?");
  50. list();
  51. search("Jones");
  52. add(firstName, lastName, email, telephone);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement