Issue with Profile Lookup task


I have an issue with my code for the Profile Lookup task, everything works except for posting back “No such property” if the property does not match, please see my code below:

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
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++){
    if (contacts[i].firstName === name){
        if (contacts[i].lastName || contacts[i].number || contacts[i].likes === prop){
            return contacts[i][prop];
        return "No such property"

return "No such contact";
// Only change code above this line

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

I have looked at the solution (which is very different from my answer) but I still cant understand why mine doesn’t work.

Any help appreciated.



From my understanding, you’re checking if the value of lastName, number, or likes is equal to prop, which is different than checking if prop exists in that person’s profile.

1 Like

I reckon you need to compare each property to prop, like so:

if (contacts[i].lastName === prop || contacts[i].number === prop || contacts[i].likes === prop) { .. }

Otherwise JS will consider each of those a separate condition, and return contacts[i][prop] as soon as one of them evaluates to a truthy value.

1 Like

Understood, that is probably why the answer is very different from mine - but shouldn’t it still return “No such property” if none of those match? (which is the case in the test values used to evaluate the task)

Changing code to the above example then causes issues:

// running tests
"Kristian", "lastName" should return "Vos"
"Sherlock", "likes" should return ["Intriguing Cases", "Violin"]
"Harry","likes" should return an array
// tests completed

Thanks for help though

Right, just realized my mistake. @natalief had it right: contacts[i].lastName would return you the value of that property (e.g. “Holmes”) and not its name, which is what prop argument represents. So you’re trying to compare “Holmes” to “lastName”.

And no, it shouldn’t return “No such property” because as long as one of your conditions evaluates to a truthy value (and “Holmes” is a truthy value) then the true block would be executed.

1 Like

Thanks for the explanation! Now I understand

I think I figured out what’s happening. If you’re trying to compare the left to the right like that, you need parentheses. contacts[i].lastName || contacts[i].number || contacts[i].likes === prop returns “Laine” (the person’s lastName value), while (contacts[i].lastName || contacts[i].number || contacts[i].likes) === prop returns false. Your current if statement is seeing if “Laine” (or the lastName value of any name you input into the function) is true. “Laine” is a string, so it always evaluates to true. This means that there is currently no condition in which your if statement will evaluate to false, so it will never return “No such property”.

(contacts[i].lastName || contacts[i].number || contacts[i].likes) === prop doesn’t work for the reason I gave in my first reply. It causes the opposite problem of the comparison without parentheses; it always returns false (and, therefore, “No such property”).

I hope this helps!

1 Like

Thanks for your in-depth reply, it makes sense and has helped me solve the issue.