I dont understand what is wrong here

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(name, prop){
for(var i=0; i<contacts.length;i++){
if(contacts[i]["firstName"]==name)
{
 if(contacts[i].hasOwnProperty(prop))
  return contacts[i][prop];

else
return "No such property";
}
return "No such contact";



}






}

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:

You’re so close.

The problem is here:

    if (contacts[i]["firstName"] == name) {
      if (contacts[i].hasOwnProperty(prop)) return contacts[i][prop];
      else return "No such property";
    }
    return "No such contact";

You are checking if the the first name matches. If the first name doesn’t match, what are you doing? You are deciding that the contact doesn’t exist and are exiting the function. After the first contact doesn’t match!

So, instead of deciding the contact doesn’t exist there, can you think of a better place? When I make that change, your code passes.

Hello kevin,
iam able to pass the test but still i have confusion why to put statement outside for loop? if you got time then please explain

are you able to tell if the contact exist or not after just looking at first contact? you need to put the return statement outside so that you actually let the loop look theough all the array

1 Like

Yes, what ieahleen said.

The other thing I might say is to pick some input data and mentally step through your algorithm, predicting what it will do. This is a great way to learn how to think.

1 Like

Hi,
i got this, thanks to both of you