Profile Look up Logic

Hello everyone, I would like to understand why my code does not work. I can’t seem to figure out the logic.

The exercise can be found here Link to exercise

My implementation of the answer was the following

function lookUpProfile(name, prop) {

    for (var i = 0;  i < contacts.length; i++ ) {
          if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
            return contacts[i][prop];
        else if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop) == false) {
            return "No such property";
        else if (name !== contacts[i].firstName) {
            return "No such contact";

When I call the function
console.log(lookUpProfile("Harry", "likes"));
I get

“No such property”.

Why is that …and yet Harry is a name in the contacts array and likes is a property of the Harry object?

Any help understanding the logic behind that code is appreaciated. Thanks.

think of which one of these is true for i = 0 and name = "Harry"

(I get “No such contact”, not “No such property”)

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.