Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. // Contact With Array
  2.  
  3. const myContacts = [
  4. ["Upin", "087870505005", "upin@gmail.com"],
  5. ["Ipin", "087123456777", "ipin@gmail.com"],
  6. ["Ismail bin Mail", "087127493726", "mail@gmail.com"],
  7. ["Jarjit Singh", "087235789642", "jarjit@gmail.com"],
  8. ["Ehsan bin Azaruddin", "087367192345", "ehsan@gmail.com"],
  9. ["Mei Mei", "087367198256", "mei@gmail.com"]
  10. ];
  11.  
  12. const showContacts = contacts => {
  13. for (let i = 0; i < contacts.length; i++) {
  14. console.log(`[${i + 1}] ${contacts[i].join(" | ")}`);
  15. }
  16. };
  17.  
  18. const insertContacts = contact => {
  19. myContacts.push(contact);
  20. console.log(`\n Contact successfully added`);
  21. showContacts(myContacts);
  22. };
  23.  
  24. const deleteContactsByNumber = numbContact => {
  25. myContacts.splice(numbContact - 1, 1);
  26. console.log(`\n Contact successfully deleted`);
  27. showContacts(myContacts);
  28. };
  29.  
  30. const filterNameByLength = maxLength => {
  31. let filterContacts = [];
  32. for (let i = 0; i < myContacts.length; i++) {
  33. if (myContacts[i][0].length < maxLength) {
  34. filterContacts.push(myContacts[i]);
  35. }
  36. }
  37.  
  38. console.log(`\n Contact successfully filtered`);
  39. showContacts(filterContacts);
  40. };
  41.  
  42. insertContacts([`Dicky Muhamad R`, "0234932042", "dickymr@gmail.com"]);
  43. deleteContactsByNumber(5);
  44. filterNameByLength(8);
  45.  
  46. // Contact With Object
  47.  
  48. console.log(`\n \n ============================================= \n \n`);
  49.  
  50. let myContactsObject = [
  51. {
  52. id: 1,
  53. name: "Alpha Avalon",
  54. phone: "+1 111 101010",
  55. email: "alpha@avalon.org"
  56. },
  57. {
  58. id: 2,
  59. name: "Betty Brave",
  60. phone: "+62 812 242424",
  61. email: "betty@braverian.com"
  62. },
  63. {
  64. id: 3,
  65. name: "Gamma Gacurio",
  66. phone: "+63 813 363636",
  67. email: "gamma@gacurio.dev"
  68. }
  69. ];
  70.  
  71. const showContactsObject = contactObject => {
  72. for (let i = 0; i < contactObject.length; i++) {
  73. console.log(
  74. `[${contactObject[i].id}] | ${contactObject[i].name} | ${
  75. contactObject[i].phone
  76. } | ${contactObject[i].email}`
  77. );
  78. }
  79. };
  80.  
  81. const createContactObject = (id, name, phone, email) => {
  82. let newContactObject = {
  83. id: id,
  84. name: `${name}`,
  85. phone: `${phone}`,
  86. email: `${email}`
  87. };
  88.  
  89. myContactsObject.push(newContactObject);
  90. console.log(
  91. `\n Contact object successfully added id: ${newContactObject.id}`
  92. );
  93. showContactsObject(myContactsObject);
  94. };
  95.  
  96. const deleteContactsObjectById = id => {
  97. let isItemDeleted = false;
  98. for (let i = 0; i < myContactsObject.length; i++) {
  99. if (myContactsObject[i].id === id && !isItemDeleted) {
  100. myContactsObject.splice(i, 1);
  101. isItemDeleted = true;
  102. console.log(`\n Contact object successfully remove id: ${id}`);
  103. showContactsObject(myContactsObject);
  104. } else if (i >= myContactsObject.length - 1 && !isItemDeleted) {
  105. console.log(`\n Contact object failed to remove. id: ${id} Not found`);
  106. }
  107.  
  108. // console.log(id, myContactsObject);
  109. }
  110. };
  111.  
  112. const filterContactsObjectByNameLength = maxLength => {
  113. let newContactObject = [];
  114.  
  115. for (let i = 0; i < myContactsObject.length; i++) {
  116. if (myContactsObject[i].name.length <= maxLength) {
  117. newContactObject.push(myContactsObject[i]);
  118. }
  119. }
  120. console.log(
  121. `\n Contact object successfully filtered max length name: ${maxLength}`
  122. );
  123. showContactsObject(newContactObject);
  124. };
  125.  
  126. showContactsObject(myContactsObject);
  127. createContactObject(4, `Dicky`, `08787503921`, `dickymr@gmail.com`);
  128. deleteContactsObjectById(2);
  129. deleteContactsObjectById(7);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement