Profile Lookup code problem

Tell us what’s happening:
This is what I could manage. Only the last 2 conditions are being satisfied. Can someone help me with it?

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 (firstName === contacts[i].firstName) {
       if (contacts[i].hasOwnProperty(prop) == true)  
         return (contacts[i].prop);
       else 
         return "No such property";
    }
    else 
      return "No such contact";     
}
  
// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Kristian", "lastName");```

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36.

Link to the challenge:

What tests are failing? What values do those expect and what results are you actually getting? How do you expect your code to work and how is it not matching your expectations?

  1. Your “No such contact” should be outside the loop (or else you return “No such contact” whenever the given firstName isn’t the first in the contact list)

  2. To return the value of the property, you use return contacts[i][prop] (if prop is equal to likes, you should return the value of the property likes not the property prop)

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

It just struck me that the reason behind my for loop running only the first iteration is my return statement as that returns the control to the calling function. So I replaced all my return statements with console.log but now I just get a blank output.

Worked like a charm. Thanks a-mt. Now that I think about it it was quite a silly mistake on my part(your 1st point). Should have paid more attention to the working of the loop.
As far as your second point is considered, why is this wrong? return (contacts[i].prop); or return contacts[i].prop;
I thought the dot notation was how we accessed the property values. Not the case with the return statement?
Also, shouldn’t the same thing work with console.log statements?

There should be brackets around prop : contacts[i][prop] not contacts[i].prop to have JavaScript interpret the value of prop (the property you gave as an argument to the function).

contacts[i].prop is like contacts[i]["prop"]
contacts[i][prop] is like contacts[i]["likes"] if prop equals “likes”.

Return and console.log would show the same output to you, however FCC’s validation script checks the value you return… so it won’t work if you don’t use return.

Got it. Thanks for your help.