Basic JavaScript: The Profile Lookup Program

Hi,

I am stuck with the profile lookup program of Basic JavaScript. The “hasOwnProperty” always returns false; I don’t understand why. Can you please tell me what is wrong with my code? I appreciate your input.

function lookUpProfile(name, prop) {
    // Only change code below this line

    var hasContacts = false;
    var contactsIndex = -1;
    var hasProperty = false;

    for (var i = 0; i < contacts.length; i++) {
        if (name === contacts[i].firstName) {
            hasContacts = true;
            contactsIndex = i;
        }
    }

    if (contactsIndex === -1) {
        return "No such contact";
    }
    if (contacts[contactsIndex].hasOwnProperty(prop)) {
        //return contacts[contactsIndex].prop;
        return "Success!"
    }
    else {
        return "No such property";
    }
   // Only change code above this line
}

You can’t access a property with dot notation when prop is a variable. Try bracket notation.

1 Like