-1

This code is about to access the property of an object If the firstname of an objet is same

Objets in an Array

const contacts = [{
    "firstName": "Akira",
    lastName: "Laine",
    number: "0543236543",
    "likes": ["Pizza", "Coding", "Brownie Points"],
  }

];

function lookUpProfile(Names, prop) {
  // Only change code below this line
  for (var i = 0; i < contacts.length; i++) {
    if (contacts[i].firstname === Names) {
      if (contacts[i].hasOwnProperty(prop)) {
        return contacts[i][prop];
      } else {
        return "No such property";
      }
    }

  }
  return "No such contact";
}

let myData = lookUpProfile("Akira", "likes");
console.log(myData);

The First one is my code, can someone tell me what's wrong with my code. And what's wrong with the logic

  • function lookUpProfile(name, prop) { for (let x = 0; x < contacts.length; x++) { if (contacts[x].firstName === name) { if (contacts[x].hasOwnProperty(prop)) { return contacts[x][prop]; } else { return "No such property"; } } } return "No such contact"; }. This one is right code, how – sketch hacker May 20 '22 at 21:10

0 Answers0