Profile Lookup what is the problem in my code!

Tell us what’s happening:

what is the problem in my code !


//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 (name !== contacts[i].firstName){
        return  "No such contact";
    }
    else if (contacts[i].hasOwnProperty(prop) == false){
        return "No such property";
    } else if (contacts[i].hasOwnProperty(prop)== true) {
      return contacts[i][prop];
    }
        
}
// 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/67.0.3396.99 Safari/537.36.

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

You are returning from your loop way too early.

For instance:

    if (name !== contacts[i].firstName){
        return  "No such contact";
    }

Tells that if the name don’t match the loop should stop and return “no contact”.

Imagine that we are looking for Harry Potter, (2nd element).
Our loop start and the first element is looked upon.

So

Akira !==Harry // true

So your loop will stop and the function will return “No such contact”.
Well, in fact there is, is just not the first.


As I see how the loop should work:

1 - loop over
2- is the name of the current element the same as we are looking for?
3- does this elements has the prop we are looking for?
4- if so return it otherwise return “no prop”
5- have we finished looping and nothing showed up? We can assume “no contact”

Hope this helps :slight_smile:

1 Like