Can anyone tell please what's wrong with my code? why is my "Profile lookup" challenge code doesn't work?

Continuing the discussion from freeCodeCamp Challenge Guide: Profile Lookup:
Why would it return “No such contact”?

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 (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name && contacts[i][prop]) {
        return contacts[i][prop]
    } else {
        return "No such contact"
    }
}

return "No such property"
// Only change code above this line
}

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

What is going on? What are you trying to do? What part do you need help with? What errors are you experiencing? What else have you tried while attempting to solve the problem?

Hi!

Your code works as expected. However, since You’re not using any output function then there seems that nothing is returned.

Change the following and it should work:

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 (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name && contacts[i][prop]) {
        return contacts[i][prop]
    } else {
        return "No such contact"
    }
}

return "No such property"
// Only change code above this line
}

console.log(lookUpProfile("Akira", "likes"));

Of course, this is only for testing purposes and not needed to complete the challenge :). By the way, if You don’t want to modify the code above or below the comments that say You should not modify it, You could write it like this:

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 (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name && contacts[i][prop]) {
        const propToReturn = contacts[i][props]
        console.log(propToReturn)
        return propToReturn
    } else {
        return "No such contact"
    }
}

return "No such property"
// Only change code above this line
}

lookUpProfile("Akira", "likes")

Thank you very much!

"lookUpProfile("Kristian", "lastName")

returns “No such contact”. while I was expecting “Vos”.

I don’t understand why.

if (contacts[i].firstName === name && contacts[i][prop])

Can you explain this logic to me?

2 Likes

"The function should check if name is an actual contact’s firstName and the given property ( prop ) is a property of that contact.

If both are true, then return the “value” of that property."

These two lines from construction made me fool.

I also tried by changing the code to

function lookUpProfile(name, prop){
// Only change code below this line
for (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name) {
        if (contacts[i][prop]) {
            return contacts[i][prop]
        } else {
            return "No such property"
        }
    }
    return "No such contact"
}
// Only change code above this line
}

// Change these values to test your function
console.log(lookUpProfile("Kristian", "eat"));

It didn’t help either.
It works for first profile name “Akira”. but I don’t know why it doesn’t work for other profile name.

@kabirsumn In this first if statement inside the for loop, what do you think will happen if it evaluates to false? For example, what if the name passed to the function is “Harry”?

1 Like

I’m sorry. that’s the thing I don’t get it. Why would “Harry” evaluate to false? I mean I’m iterating over “contacts” array. so it should iterate through all items in the “contacts” array. right? isn’t it? Please tell me what I’m missing?

The for loop starts iterating with i = 0, so your if statement checks if contacts[0].firstName is equal to “Harry”. The first object’s firstName value is “Akira” which is definitely not equal to “Harry”. That is why it evaluates to false and then the next line after that if statement is `return “No such contact” and the function exits. It never makes it past i = 0.

You are 99% of the way there. You do not need to write any more code. You only need to move one line to a different location.

1 Like

Thank you very much! It was very helpfull.