Advertisement
Guest User

Profile Lookup

a guest
Apr 16th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. //Setup
  3. var contacts = [
  4.     {
  5.         "firstName": "Akira",
  6.         "lastName": "Laine",
  7.         "number": "0543236543",
  8.         "likes": ["Pizza", "Coding", "Brownie Points"]
  9.     },
  10.     {
  11.         "firstName": "Harry",
  12.         "lastName": "Potter",
  13.         "number": "0994372684",
  14.         "likes": ["Hogwarts", "Magic", "Hagrid"]
  15.     },
  16.     {
  17.         "firstName": "Sherlock",
  18.         "lastName": "Holmes",
  19.         "number": "0487345643",
  20.         "likes": ["Intriguing Cases", "Violin"]
  21.     },
  22.     {
  23.         "firstName": "Kristian",
  24.         "lastName": "Vos",
  25.         "number": "unknown",
  26.         "likes": ["Javascript", "Gaming", "Foxes"]
  27.     }
  28. ];
  29.  
  30.  
  31. function lookUpProfile(firstName, prop){
  32. // Only change code below this line
  33. if (checkName(firstName)) {
  34.   if (checkProp(prop)) {
  35.     return contacts.prop;
  36.   }
  37.   else {
  38.     return "No such property";
  39.   }
  40. }
  41.   else {
  42.     return "No such contact";
  43.   }
  44.  
  45.   function checkName(firstName) {
  46.     var i = 0;
  47.     while (i < contacts.length) {
  48.       if (contacts[i].firstName == firstName) {
  49.         return true;
  50.       }
  51.       else if (i == contacts.length - 1 && contacts[i].firstName != firstName) {
  52.         return false;
  53.       }
  54.     i++;
  55.     }
  56.   }
  57.   function checkProp(prop) {
  58.     var i = 0;
  59.     while (i < contacts.length) {
  60.       if (contacts[i].prop == prop) {
  61.         return true;
  62.       }
  63.       else if(i == contacts.length - 1 && contacts[i].prop != prop) {
  64.         return false;
  65.       }
  66.       i++;
  67.     }
  68.    
  69.   }
  70. // Only change code above this line
  71. }
  72.  
  73. // Change these values to test your function
  74. lookUpProfile("Akira", "likes");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement