Oletem
9/30/2017 - 12:48 PM

Profile Lookup

We have an array of objects representing different people in our contacts lists.

A lookUpProfile function that takes firstName and a property (prop) as arguments has been pre-written for you.

The function should check if firstName is an actual contact's firstName and the given property (prop) is a property of that contact.

If both are true, then return the "value" of that property.

If firstName does not correspond to any contacts then return "No such contact"

If prop does not correspond to any valid properties then return "No such property"


//Setup
var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    }
];


function lookUpProfile(firstName, prop){

for(var x =  0; x<contacts.length; x++){// Проходись по всему списку, в конкретный елементы нырять не надо, в них ныряем внутри if
  
  if(contacts[x].firstName === firstName){//Внутрь списка, к конкретной переменной
    
    if(contacts[x].hasOwnProperty(prop)){// "likes": ["Javascript", "Gaming", "Foxes"] - справа как раз пропы, их и ищем методом
      
      return contacts[x][prop];// выводим пром, через точку нельзя, contacts[x].prop - будет искать объект только с таким названием, как prop, а не типом
    }else{
      
      return "No such property";//Первым написан отказ в проперти, потому что осутствие имени парсится в конце, заканчивает луп в целом
    }
  }
}
  return "No such contact"; //Выводится после прохождения всего цикла. парсинга контактов
}

// Change these values to test your function
lookUpProfile("Akira", "likes");

/*---------------------------------------мое решение без hasOwnProperty()------------------------------------------------------*/

var contacts = [
    {
        "firstName": "Akira",
        "lastName": "Laine",
        "number": "0543236543",
        "likes": ["Pizza", "Coding", "Brownie Points"]
    },
    {
        "firstName": "Harry",
        "lastName": "Potter",
        "number": "0994372684",
        "likes": ["Hogwarts", "Magic", "Hagrid"]
    },
    {
        "firstName": "Sherlock",
        "lastName": "Holmes",
        "number": "0487345643",
        "likes": ["Intriguing Cases", "Violin"]
    },
    {
        "firstName": "Kristian",
        "lastName": "Vos",
        "number": "unknown",
        "likes": ["Javascript", "Gaming", "Foxes"]
    }
];



function lookUpProfile(firstName, prop){

  for(var i = 0; i<contacts.length; i++){

    
  if(contacts[i].firstName == firstName){
    
    if(contacts[i][prop]==firstName[prop]){
      
     return "No such property";/*How does it parses as correct, does it return "No such property" 
     
     only if (contacts[i][prop]==firstName[prop]) evaluetes to false, skipping it otherwise? */
     }
    
        return contacts[i][prop];
  }// nested if part ends returning props array
      
  }//for part ends 
  
  return "No such contact";// Last output, because for loop has to parse all contacts parts before output it
  }

// Change these values to test your function
lookUpProfile("Harry", "likes");