Profile Lookup what else?

Profile Lookup what else?
0.0 0

#1

Tell us what’s happening:

Your code so far

//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){
// Only change code below this line
  for(var i = 0; i < contacts.length; i++){
    
    if(contacts[i].firstName != firstName){
      return "No such contact";
    }else if(contacts[i].prop != prop){
      return "No such property";
    }else{
      return contacts[i].prop;
    }
  }

   
// Only change code above this line
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:57.0) Gecko/20100101 Firefox/57.0.

Link to the challenge:


#2

First let me get to two technical points before I get to the logic,

(1) Remember that if the property of an object is a variable, meaning you don’t know what it is before hand, you can only access it using square brackets, so contacts[i].prop will not work since prop is a variable and not the actual name of the property, therefore you must use contacts[i][prop] to access it.

(2) The method to check if an object has a property is obj.hasOwnProperty(prop) which will return true/false, therefore you would use contacts[i].hasOwnProperty(prop) to check if the variable property exists, if you already know the name of the property that you want to check then you simply use the string literal, for instance, contacts[0]. hasOwnProperty("firstName") will return true, whereas contacts[0]. hasOwnProperty("age") would return false.

Now we got that out of the way , take a look at the logic in the for loop of your lookUpProfile function, on the first instance that contacts[i].firstName != firstName it will exit the loop prematurely without looking at the rest of the objects for the firstName, using the above technical definitions try to fix this logic first.

Feel free to ask more questions if stuck.


#3

@Dereje1 thank you i solved it :slight_smile: