Need help with Basic JavaScript: Profile Lookup

Hello guys!
I’ve been trying to get pass this for the past hours, but I can’t understand why the code is not running as I think it should. :disappointed:
I’m getting the vibe that my “returns” are wrong, but I can’t understand actually what is happening.
Please help.

Code
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 && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        } else if (contacts[i].firstName !== name) {
            return "No such contact";
        } else if (contacts[i].hasOwnProperty(prop) === false) {
            return "No such property";
        }
    }
}

You have a little bit of a logical issue here…

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

Look carefully at your first else if. What happens the very first time that you hit a name that is not a match for name? Is that what you want?

What about the second else if? What is the exact condition, in words, for when you want that return statement?

1 Like

I think I get what you’re saying… Like, if the first name is in the “contacts”, but in the second array, my “else if” will give the “No such contact” and my for loop will end, givin no chance to complete a second loop and maybe find the name, right?
About the second “else if” I don’t totally think I get… should I just use the “else” followed by the “return”?

On the first count, exactly right. The first time you hit a contact that does not match name, you return "No such contact", but I think you want to wait until you’ve checked all names.

To ask the second part in a different way, what two things need to be true for you to return "No such property"?

I can only return “No such property” if I do have a valid name, so I must use that on my condition as well, right?

Exactly. You want a matching name but no matching property, which, like you said, means adding the check for the matching name to that conditional.

1 Like

Great! Thank you very much for your guidence, it was very well done. :grin:

1 Like