Basic Javascript: Profile Lookup Logic Error, Syntax Error, or Both?

Tell us what’s happening:
I am not entirely sure where to go from here. I assume there is a problem in my logic, but I can’t seem to take a step back and figure out what I am missing.

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

lookUpProfile("Akira", "likes");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36.

Challenge: Profile Lookup

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

I’ll help you get started with some troubleshooting tips:

  1. Use console.log() on values like contacts[name][prop]. If it doesn’t produce what you are expecting (in this case its producing an error), then likely you are not writing what you want to write.

  2. consider using dot notation, when possible.

  3. for (prop == contacts[name][prop]), consider using the .hasOwnProperty function since there are multiple properties, you need to check each one to see if it matches prop. Your code right now assumes that there is only one property. You could write a for loop but thats more work than you want to do.

  4. think about what you’re using i for. you want to loop through each object within an array of objects. right now you’re not doing that.

Thanks for this! I feel like I am awfully close, but still no cigar. I am not passing the first three tests.
“Kristian”, “lastName” should return “Vos”
“Sherlock”, “likes” should return [“Intriguing Cases”, “Violin”]
“Harry”, “likes” should return an array


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

Nevermind, just figured it out with the below solution!

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

this is wrong. contacts is an array, it doesn’t have a firstName property. This check doesn’t have sense.

to get better, you need to fix this too, even if it already pass the tests

Good catch! If I remove the if statement and just put return “No such contact”, that also passes. That’s a final catch-all, correct? See below.

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

it seems you put it outside the function.
It doesn’t need to be on a code block of its own
If it is the last line of the function it is perfect