Profile Lookup-attempt

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] == contacts[i].prop){
      return prop[i];
    }
    else if (contacts[i].firstName !== contacts[i].prop ){
      return "No such contact";
  }
    else{
      return "No such property";
    }
  }
// 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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/profile-lookup

What are you trying to compare here?

contacts[i] == contacts[i].prop

I was horribly wrong.Actually, I couldn’t understand the question properly.I was trying to compare firstName with its prop, instead I had to compare firstName with contacts[i].firstname.

You have to make you comparisons inside the for loop, to check each item in the array.

I would do it like this: First check if each contact’s firstname matches the given firstname (or print no such contact) and, if so, check if it has its own property “prop” (or print no such property) and if it has, print that contact’s property.

Hope it helps.

1 Like
function lookUpProfile(name, prop){
// Only change code below this line
for (var i = 0;i<contacts.length;i++){
  if(name == contacts[i].firstName){
     if(contacts[i].hasOwnProperty(prop) == true){
    return contacts[i][prop];
  }
  else{
      return "No such property";
  }
  }
  else{
    return "No such contacts";
  }
 
}
// Only change code above this line
}

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



```**I am stack over here .. I can not understand what is the problem ? Please help me out**

Your code seems to be correct. All you need to do is fix the “hasOwnProperty()” function.It will automatically be set to true if the the firstname has a property.Give it a try and let me know if you still face any error.

what is the problem here ?

then how to break the condition to return “no such contacts”

oh I fond the prob thanks randelldawson