Build a Profile Lookup - Build a Profile Lookup

Tell us what’s happening:

I’m struggling with getting the ‘No such contact’ answer to work. This is the only one that’s failing. I’m aware that it needs to be outside of the loop but it doesn’t get past ‘/no such property’.

I don’t think it’s an issue with returning because the same thing happened when I created a variable and reassigned it each time and returned the variable at the end.

I can’t get my head around the logic for coming out of the loop to assess whether the contact exists or not.

Thanks in advance

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"],
  },
];

const lookUpProfile = (name, property) => {
  for (let i = 0; i < contacts.length; i++) {
    if (contacts[i].firstName === name && property in contacts[i]) {
      return contacts[i][property];
    }
  }
  if (!(property in contacts)) {
    return "No such property";
  } else {
    return "No such contact"
  }
}

console.log(lookUpProfile("Akira", "address"));

Challenge Information:

Build a Profile Lookup - Build a Profile Lookup

You are looping twice. What are you trying to find inside the loop? Once you find a match, what do you need to do then?

Break out of the loop?

What are you looping to find? What needs to be matched before you do anything else? You only need one loop.

const lookUpProfile = (name, property) => {
    for (let i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName === name && property in contacts[i]) {
            return contacts[i][property];
        }
        if (!(property in contacts)) {
            return "No such property";
        } else if (contacts[i].firstName !== name && !(property in contacts)) {
            return "No such contact";
        }
    }
};

console.log(lookUpProfile("Bob", "potato"));

Thank you for your help. I’m looping to find whether name argument matches the contacts array and if so, then matching it with the property argument. And if the property doesn’t exist return that and if the contact doesn’t exist, return that

I began using chat gpt to help with this but I feel like it’s restricting my ability to break the problem down properly. I may need to do some easier challenges again before and come back to this

here you are checking if property is in contacts, does contacts have any of the properties you search for? what is contacts?

What happens if the first contact in your loop does not match name and does not have property? Does your loop stop there, or does it continue running?

contacts is an array of objects, I’ve changed it to contacts[i] which isn’t working but it feels like it would work because it’s checking on each iteration if the property is in the object

add console.log(i) as first thing inside the loop, how many times it is iterating?

const lookUpProfile = (name, property) => {
    let result = null
    for (let i = 0; i < contacts.length; i++) {
        if (contacts[i].firstName === name && property in contacts[i]) {
            result = contacts[i][property];
        }
        if (!(property in contacts[i])) {
            result = "No such property";
        } else if (contacts[i].firstName !== name && !(property in contacts)) {
            result = "No such contact";
        }
    }
    return result;
};

console.log(lookUpProfile("Bob", "potato"));

I’ve changed it so it’s not returning out but it still isn’t getting to the bottom line

It’s iterating four times

Please answer the first question.

I got it thanks! I know you don’t let the answer be posted but I used break after the first two iterations as well as removing the condition for the else statement at the end. I also checked that the firstName matched for the property part of the if statement.

Thanks for your patience, much appreciated!