Profile Lookup endeavour

Hi. I’m stuck on Profile Lookup.

Here’s 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 (firstName == contacts[i].firstName) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][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("Akira", "likes");

As far as I can ascertain, I’ve followed the instructions to the letter. I’d appreciate any help.

You have not declared firstName variable anywhere in code and it is neither in arguments.
Replace this firstName with name variable as you have in arguments.
See if that helps.

1 Like
    // Only change code below this line
    for (var i = 0; i < contacts.length; i++) 
    {
        if (name === contacts[i]["firstName"]) {
            if (contacts[i].hasOwnProperty(prop)) {
                return contacts[i][prop];
            } 
            else 
            {
                return “No such property”;
            }
        } 
        else 
        {
            return “No such contact”;
        }
    }
    // Only change code above this line
}

Remove this else statement.

Put this line after the for loop:

That should help.

1 Like

Hi. Is it because “No such contact” should be returned once i is no longer less than the length of contacts?

To attempt to answer your second question, is it because i < contacts.length means there are still contacts to run through before you reach the end of the contacts list?

Thank you @camperextraordinaire and @aditya_p for your help.