Profile Lookup -- what wrong am i doing 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){
// Only change code below this line
for(var i=0; i<contacts.length; i++){
    if(name === contacts[i].firstName && prop === contacts[i][prop]){
        return contacts[i][prop];
    } else if(name !== contacts[i].firstName){
        return "No such contact";
    } else if(prop !== contacts[i][prop]){
        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 (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://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

Think about your logic here.

for(var i=0; i<contacts.length; i++){
    if(name === contacts[i].firstName && prop === contacts[i][prop]){
        return contacts[i][prop];
    } else if(name !== contacts[i].firstName){
        return "No such contact";
    } else if(prop !== contacts[i][prop]){
        return "No such property";
    }
}

You’re looping through each one. OK.

In the first if you check if the name is equal to that persons name - that part is OK - and you check if the prop === contacts[i][prop]). But the prop being sent is “likes” and the contacts[i][prop] is [“Pizza”, “Coding”, “Brownie Points”] - how are those ever going to be equal. But I think that second part is unnecessary.

What is the logic here? As you loop, you check if each name exists. If it does exist, then you check if that prop exists and return the prop if it does. If the prop doesn’t exist, then you return “No such property”. And if you get through the loop and never found that name, then you know that that contact doesn’t exist, so then you return “No such contact”.

Let us know if this hint is insufficient.

1 Like

I got it. But, I was so wrong here!! I was doing fine up until now.
So what does that mean? Have I not understood somethings and shouldn’t move on?
Does a lot of people face problems on this exercise?
How can I avoid making such mistakes in the future?

I’m sure a lot of people have. And if not this, then on some other algorithm. Don’t let it discourage you. Don’t focus on the mistakes you’re making, focus on what you’re learning. Yes, this is hard now, but the more algorithms you do, the easier they get. Every mistake you make means that you are learning something - be proud of them!

How can I avoid making such mistakes in the future?

No one can avoid all mistakes. But the more you do these, the better you get and the better you get, the fewer mistakes you make. Just put in the time, you’ll get there.

Thanks man. A couple of things in particular bugged me is that when I saw the line…

The function should check if name is an actual contact’s firstName and the given property (prop) is a property of that contact.

…in the instructions I immediately thought of && operator and secondly I didn’t think of hasOwnProperty() method to check.
Is making a mental checklist of mistakes the only solution or is there some other hack experienced folks like you use?

I teach guitar lessons as my day job (until I get a coding job). A lot of students want to learn improv, soloing. Most of them are timid and afraid to play a bad solo. I always tell them, “If you want to play good solos, first you have to play a lot of bad solos.” I think it’s the same with coding. This is not a “natural” way to think. It takes time. Just keep coding and it will get easier. But yes, try to learn from each mistake - that is the point. I don’t know about a “checklist” - it’s just going to happen naturally, just try to really understand why what you did was a mistake and how it should be done.

1 Like