Advertisement
Projucti

add new contact

Nov 26th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. Instruction:
  2. We'll be creating a function that allows us to add our new friend to the address book.
  3. Create a function called add with the parameters firstName, lastName, and email, phoneNumber.
  4. In this new function, you want to create a new contact object like bob and mary. Instead of having this object's property values be filled with strings though, set them to the appropriate function parameters passed in.
  5. Add this new contact object to the contacts array.
  6. Call add with whatever first name, last name, phone number, and email arguments you like.
  7. Make sure you call the list function, to check if your new entry is added. And delete any other function that logs output in the console, i.e 'search' function.
  8.  
  9. Run the code!
  10.  
  11.  
  12. var bob = {
  13. firstName: "Bob",
  14. lastName: "Jones",
  15. phoneNumber: "(650) 777-7777",
  16. email: "bob.jones@example.com"
  17. };
  18.  
  19. var mary = {
  20. firstName: "Mary",
  21. lastName: "Johnson",
  22. phoneNumber: "(650) 888-8888",
  23. email: "mary.johnson@example.com"
  24. };
  25.  
  26. var contacts = [bob, mary];
  27.  
  28. function printPerson(person) {
  29. console.log(person.firstName + " " + person.lastName);
  30. }
  31.  
  32. function list() {
  33. var contactsLength = contacts.length;
  34. for (var i = 0; i < contactsLength; i++) {
  35. printPerson(contacts[i]);
  36. }
  37. }
  38.  
  39. function add(firstName, lastName,email, phoneNumber){
  40. contacts[contacts.length] = {
  41. firstName: firstName,
  42. lastName: lastName,
  43. phoneNumber: phoneNumber,
  44. email: email
  45. };
  46. }
  47.  
  48. add("me","u","a@gamail.com",1234344);
  49. list();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement