Why isn't this code working?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**
// Setup
const 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
var nf=false,pf=false;
for(let i=0;i<contacts.length;i++){
  if(contacts[i].hasOwnProperty(name)==true){
    nf=true;
  }
  if(contacts[i].hasOwnProperty(prop)==true){
    pf=true;
    var c=i;
  }
}
if(nf==true&&pf==true){
  return contacts[c][prop];
}else if(nf==false){
  return "No such contact";
}else if(pf==false){
  return "No such property";
}
// Only change code above this line
}

lookUpProfile("Akira", "likes");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0

Challenge: Profile Lookup

Link to the challenge:

It’s always a good idea to say “What is not working”.
In this case I imagine you are getting the values from the last contact?
Consider your code in the for loop, your check on prop is not dependent on your check for name.
One item is that the “name” parameter holds the value of the name you are checking that the contact has the property in the name.
contacts[i].hasOwnProperty(name)contacts[i].hasOwnProperty("Akira")
Also, only when the the name property matches should you check the prop value.

1 Like

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