Profile Lookup alternative solution (not working)

Tell us what’s happening:

Hello community!

I wrote down this code to solve the “Profile Lookup” problem but when I try to “run the test” apparently I’m not satisfying the following conditions:

[spoiler]

* "Kristian", "lastName"`  should return  `"Vos"`

* "Sherlock", "likes"`  should return  `["Intriguing Cases", "Violin"]`

* "Harry","likes"`  should return an array

Can you catch some error on my code?

Thanks in advance for your help! BR.

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

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36.

Challenge: Profile Lookup

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

when i is 0 your function is always returning something, never checking the other elements in the array

1 Like

Hello ieahleen. Thanks for your reply!

I don’t get what you point off. When I go through the code step by step I do not find anything wrong with the IF condition --> if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)) <-- For example, if the input is (“Kristian”, “lastName”), it should return (“true” AND “true”) and execute the internal step.

Could you give me some more details on how you noticed there is always returning something with i = 0?

Thanks in advance. BR.

the first time that if statement is met, it is contacts[0] that is checked. the if and else if statement conditions are false,
so the else block is executed, which contains return "No such contact"; , the return statement stops the function and return the value

1 Like

Thanks ieahleen! I took a moment to understand it but finally I did it. You were right, function was always returning a result with the first iteration.

I fixed the code like this:

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 property";
        }
    }
    return "No such contact"; 
}

It works fine! Thank you for your support.

BR.

Thanks for your support Randell, it was helpful!

I adjusted the code so it’s working fine now. I left the code snippet in a comment below if somebody want to check it out.

BR.