Basic Javascript: Lookup Profile - Code review

Hi there, I would like to know your opinion about how optimize my solution for that exercise. I would like to see how you solved that or if exists another more clean way to solve this exercise. Thanks.


My Solution:

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

lookUpProfile("Akira", "likes");

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Thank you.

1 Like

Oh, Iā€™m very sorry for that.

Did you look at the solutions provided?

You could make the code a bit more dry by only making the following comparision one time vs. twice (in two separate if statements).

contact.firstName == name
1 Like

Thanks @RandellDawson