Advertisement
ErolKZ

Untitled

Jul 30th, 2021
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1.  
  2. // Setup
  3.  
  4. let contacts = [
  5.  
  6. {
  7. "firstName": "Akira",
  8. "lastName": "Laine",
  9. "number": "0543236543",
  10. "likes": ["Pizza", "Coding", "Brownie Points"]
  11. },
  12. {
  13. "firstName": "Harry",
  14. "lastName": "Potter",
  15. "number": "0994372684",
  16. "likes": ["Hogwarts", "Magic", "Hagrid"]
  17. },
  18. {
  19. "firstName": "Sherlock",
  20. "lastName": "Holmes",
  21. "number": "0487345643",
  22. "likes": ["Intriguing Cases", "Violin"]
  23. },
  24. {
  25. "firstName": "Kristian",
  26. "lastName": "Vos",
  27. "number": "unknown",
  28. "likes": ["JavaScript", "Gaming", "Foxes"]
  29. }
  30. ];
  31.  
  32.  
  33. function lookUpProfile(name, prop) {
  34.  
  35. // Only change code below this line
  36.  
  37.  
  38. let counter = 0;
  39.  
  40. let counter2 = 0;
  41.  
  42.  
  43.  
  44.  
  45.  
  46. for (let i = 0; i < contacts.length; i++) {
  47.  
  48. let currentObject = contacts[i];
  49.  
  50.  
  51.  
  52. if (currentObject.firstName === name) {
  53.  
  54. counter++;
  55.  
  56. }
  57.  
  58.  
  59. for (let j = 0; j < Object.keys(currentObject).length; j++) {
  60.  
  61. if (prop === Object.keys(currentObject)[j]) {
  62.  
  63. counter2++;
  64.  
  65. }
  66.  
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. if (name === currentObject.firstName && counter2 > 0) {
  77.  
  78. console.log(currentObject[prop]);
  79.  
  80. }
  81.  
  82.  
  83.  
  84.  
  85. }
  86.  
  87.  
  88. if (counter === 0) {
  89.  
  90. console.log('No such contact');
  91.  
  92. }
  93.  
  94.  
  95. if (counter2 === 0) {
  96.  
  97. console.log('No such property');
  98.  
  99. }
  100.  
  101.  
  102. // Only change code above this line
  103.  
  104.  
  105. }
  106.  
  107.  
  108. // lookUpProfile("Bob", "number");
  109.  
  110. lookUpProfile("Kristian", "lastName");
  111.  
  112. // lookUpProfile("Akira", "address");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement