I am doing the Basic JavaScript: Profile Lookup and I can't figure out why my function isn't working, can you help me?

I know people have asked this question several times but I need some help identifying why my function doesn’t work and if I am just misunderstanding how if statements work.

The array of objects for reference:

// 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"]
    }
];

My function:

function lookUpProfile(name, prop){
    var doesItExist;
    for (let user of contacts) {
        if (user["firstName"] === name) {
            doesItExist = user.prop
        }   if (user[name] !== name) {
            doesItExist = "No such contact";
        }       if (user.hasOwnProperty(prop) === false) {
            doesItExist = "No such property";
        }
    } return doesItExist;
} 
1 Like

I don’t think your nesting is working how you expect it to. You want something that does this …

  1. Look at each user in turn …

  2. If the users name matches …

  3. … find out whether they have “prop”

  4. If they have “prop”, return its value …

  5. … otherwise return “no prop”

  6. Finally, if we’ve looped through all the users and haven’t returned - that means we didn’t match the name, so return that there’s no such user

From what I can imagine you want to look up a name and a property.

If the name isn’t there, return no such contact

If the name is there, but not the property, return no such property

If the name is there, and the property, return the property.

If you agree, try to re-write what you have.
Right now, it’s not doing that.

This is just how I think of it, probably not completely correct. But it may help…

function lookUpProfile(name, prop){
   // Input: name, prop: strings
   // return prop, or no such prop, or no such name.
    for (let user of contacts) { 
        //loop over array of contacts
        if (user["firstName"] === name) { 
           //if name is there
           if(user[prop]) return user[prop] //if prop is
           else return `${prop} doesn't exist` //if isn't
        }  
        else return `${name} doesn't exist`; //if name is not there
      }      

I agree with the answers you’ve gotten already. I just wanted to note, shen you’re trying to figure out why your code isn’t working, it really helps to take a concrete example. So:

If you look at what your code is actually doing, let’s say you try to look up “Harry”. The loop will find Harry, and it will do something with doesItExist, BUT right after that it will find Sherlock, so doesItExist will get changed back to “No such contact”.

This problem suggests that, like @Minsky suggested, you need to use a “return” as soon as you find the user, not at the end of the whole loop.

Hope this helps! Good luck!

And also, welcome to the forum!!

try to looking at this - what are the two sides of the comparison? does it make sense?

other issue - you have three if stattements, they are all evaluated indipentently, even if your formatting make it seems more like an if/else if chain

I guess I was trying to compare whether the name argument passed into the function was the same as the name property in the object. And I don’t really understand how the if statements work then. You are saying they all run at the same time?

Hey thanks for the welcome! So what you’re saying is that the loop finds “Harry” but because the if statement doesn’t end it just continues the loop?

if we substitute the values for one specific case, it becomes user.Harry === "Harry"

I am not trying to sound dumb or something but why is that incorrect? I thought when you were making comparisons sometimes they match.

is Harry ever a property key? (remember that properties have the key: value format)
has a property ever have the same key and the same value?
that’s the issue

Oh no it isn’t. This is all so confusing!

what could have value of Harry in the object?

Oh I see, I had written it totally wrong. Thank you.