Profile Lookup - Using Map

Tell us what’s happening:
Can anyone review my code. I checked it’s working fine. But the test cases are failed

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

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

Your browser information:

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

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

Two major reason:

1- you are not returning anything from your function, the tests expect some return value.

2 - map is a method made to “update” each entry of an array.
It returns a new array with the new values inside:

const newMappedArray = oldArray.map(() => updatefn());

So, if you change your code to actually return the new array from map:

lookUpProfile("Kristian", "lastName");
// [ 'No such contact', 'No such contact', 'No such contact', 'Vos' ]

And that’s not the desired output.
So, are you sure map is the best tool for the job? :slight_smile:

2 Likes

Thanks@Marmiz, Now I am clear i am going to continue with filter instead of map :slight_smile: