Build a Profile Lookup - Build a Profile Lookup

Tell us what’s happening:

I’m confused why this works for some prompts like lookUpProfile(“Sherlock”, “likes”) while it doesn’t work for lookUpProfile(“Kristian”, “lastName”).

Your code so far

let 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, property) {
  let result;

  for (let i=0; i<contacts.length-1; i++) {
    if (contacts[i]["firstName"] === name && contacts[i].hasOwnProperty(property)) {
      result = contacts[i][property];
    } else if (contacts[i]["firstName"] !== name) {
      result = "No such contact";
    } else if (contacts[i].hasOwnProperty(property) === false) {
      result = "No such property";
    }
  }

  return result;
}

console.log(lookUpProfile("Kristian", "lastName"));


Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Build a Profile Lookup - Build a Profile Lookup

run your code with this https://pythontutor.com/ and see what happens line by line

@ILM Thank you! Used the tool to figure it out

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