Profile Lookup JS problems

Tell us what’s happening:
I’m getting that first else if line working, but something is fundamentally very wrong with the rest of the code. I’m not sure what I’m doing wrong and kinda struggling to make out how everything is supposed to be working. What am I missing here? Thank you for any help, it is greatly appreciated.

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 x = 0; x < contacts.length; x++) {
    if (contacts[x]["firstName"] == name && contacts[x].hasOwnProperty(prop) == true) {
        return contacts[x][prop];

    } else if (name != contacts[x]) {
        return "No such contact";

    } else if (contacts[x][prop] != name){
        return "No such property";
    }

    
}
// Only change code above this line
}

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

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

Remember that as soon as a return statement executes, the function is exited. That means that your code only ever checks the first contact in the list.

2 Likes

Oh so the problem is actually with the “No such contact” line right? That return is keeping the function from going forward? So I have to find a way to segregate those last two returns lines to be more specific when executed?

Sounds like you’re on the right track. Happy coding!

1 Like

Errr I’m not sure how to format the code in replies, but I tried this adding another bracket to close out the first part of the function to continue to the if statement but it’s still giving me issues. I’m actually not sure how to stop it from executing the no such contact part. I figured that in the for loop there if it fails it would just go to the if statement and it’d be okay right? But that’s not working.

    if (contacts[x]["firstName"] === name) {
        if (contacts[x].hasOwnProperty(prop) === true) {
            return contacts[x][prop];
             }           
         }
           if ( name != contacts[x]) { return "No such contact";
         }

           else { return "No such property";
         }
    }

Consider this, when can you say for sure that you have checked all users in the object, and so you can say that there is no such contact?

I tried switching the contact and property statement and adding this instead

 }           
         
           if (prop != name) { return "No such property";
         }

           else { return "No such contact";
}

And now everything checks off except no such contact. :confused:

I actually got it to work by just saying return “No such contact”; at the bottom of the code area outside of a lot of the brackets. Why did that work? I’m having trouble understand it.

Because that code executes after the loop is all done. If the loop is all done without returning, then it must mean that the contact doesn’t exist.

1 Like

Say, if in your function call "lookUpProfile(“C”, “likes” ); argument “C” doesn’t match the contacts[0] 's “firstName” property, you don’t want your function call to return “No such contact” statement, rather you want your for … loop to keep running and check the next item, which is contacts[1]. Hence, you put the return “No such contact” outside of { for…loop }. Thus, you make sure that your function call lookUpProfile (argument, argument), get checked against all the items in the array, before returning “No such contact / argument” statement. Hope it makes sense. If not, please feel free to ask.