Profile Lookup still not working

What’s wrong with my code please:

My 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){
// Only change code below this line
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 contact";
    } 
}
}
return "No such property";

// Only change code above this line
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 6.0; 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.136 Mobile Safari/537.36.

Here is the link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/

return means you exit the function. So in your code, if the first profile is not the one you’re looking for, the loop finishes and the function exits regardless of if the profile is in the array or not.

Hi DanCouper, i actually tried this several times and it still doesn’t seem to work, i then had to go check on the hint and this was exactly what the hint provided as well. Does it mean that the hint provided by FCC for this particular profile lookup isn’t correct or there’s an invisible mistake i made but probably can’t see? I really need your help please. Thanks

Look very carefully at what the return values are and where they are in the hint: your code is not the same.

Hi DanCouper, thank you very much for your replies, the mistake i made was returning “No such contact” in the first else statement instead of “No such property” and vis visa. I’m glad for FCC haven provided a forum like this to help us when we get stuck

1 Like

It’s a very easy mistake to make! Quite subtle, but the property one you’re fine to return straightaway because it means that the input to the function is wrong, whereas the contact one you return once you’ve gone through the whole array - if you’ve got to the end, you know the contact isn’t there, so fine to return