What's wrong with my code?It doesn't work!

I can’t figure out what is the problem.Please guide me.

//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(firstName, 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");

You return "No such contact" too early.

1 Like

There is still bug bro.

function lookUpProfile(firstName, 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";
    }   
  }
}
  return "No such contact";
// Only change code above this line
}

contacts[i].prop This line will look for a property called prop.

:confounded: I’ve found the bug.The problem is with accessing the object’s property.
We can’t use the dot if we don’t know the exact name of the property.

This won’t work.

This will work. Anyway thank you so much bro :yum:

1 Like

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

1 Like

Thanks! I was trying to understand the difference.