Profile Lookup- can you help explaining why my code is not 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
for (let i = 0; i<contacts.length; i++){
  if (contacts[i].firstName === name &&contacts[i].firstName.hasOwnProperty(prop)){
  return contacts[i].firstName[prop];
} else if (contacts[i].firstName !== name){
  return 'No such contact'
} else {
  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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

So there are three reasons why your function should return:

  1. the contacts[i] is the appropriate person, and they have the right property - you return that property.
  2. the contacts[i] is the appropriate person, but they don’t have that property. You return the ‘no such property’ message.
  3. the entire contacts array doesn’t contain the appropriate person. In that case, you return the ‘no such person’ message.

Right now, your code does this:

  1. does the first person match, and have the property? return it.
  2. does the first person not match? return the ‘no such contact’ message.
  3. otherwise (if the first person does match but doesn’t have the property) return the ‘no such property’ message.

So in every case you’re only testing the first person. Is that your intended logic?

Thank you so much for your reply.
can you please tell me why it’s not looping in the whole array and it’s just checking the first person?

That was the point of my comment. Think about the logic here:

function lookupProfile(name, prop){
  start looping through contacts
    if the current contact has a matching name: {
      if the matching contact has the given property, 
        return that property.
      if not, return the 'no such property' message.
    } 
    if the current contact does not have a matching name,
      keep looping.
  Have we looped through all the contacts without a match?
    return the 'no such contact' message.
}

that’s the logic I see going on here. Inside the loop checking contact names, you only return something if you find a matching contact, either they have the property or they don’t.

But if you go through all the contacts and don’t find a match (so outside the loop), then you do the ‘no such contact’ message.

Right now, for every case, you’re returning from the first user. You aren’t allowing the “contacts[i].firstName!==name” to drop through, which needs to happen.

1 Like

Thanks I appreciate your help so much, I got it now. :smiley:

1 Like