Profile lookup assistance

Hey guys I am stuck at profile lookup.
I understood I have to iterate through the first array, and second array too.
when I try to console(log[i][j]);
I get undefined. how come? and what am I doing wrong ?
Thanks for help.

  **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++) {
for (let j = 0; j < contacts[i].likes.length; j++) { 
  console.log(contacts[i][j]); 
  if (name===contacts[i].firstName && prop===contacts[i].likes[j]) {
    return contacts[i].likes[j];
  }
}
}

// 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.45 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

You shouldn’t need two loops.

You only need the one loop over all contacts. After that you need to check if the property exists.

I thought about it then I read “then return the “value” of that property.” if I want to access it shouldn’t I need to iterate first?

If the value is an array, you return the whole array.

1 Like

got a question

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

Why does my 2nd if statement cancels the first code? ty.

i see you are typing the answer but I think i got it on my own.

A return statement immediately stops your function, even if the loop would have more iterations left.

I have another question for you

When If name does not correspond to any contacts , the question is like this does it mean
name does not correspond to contacts[0][1][2][3]? not just contacts.firstname

You only need to check for a matching firstName. If no name matches once you have checked every name, then you can return the No such contact phrase.

1 Like

so I’ve completed it

for (let i = 0; i < contacts.length; i++) {
    if (name===contacts[i].firstName && contacts[i].hasOwnProperty(prop)) {
     return contacts[i][prop];
    }

else if (name===contacts[i].firstName && prop!==contacts[i].prop) {
  return "No such property";
}

}

if (name!==contacts.firstName) {
    return "No such contact";
  }

Just to make sure even tho I have completed the challenge, because of the loop and the first “if” that checks for the correct name, is that why it’s okay to type the code like this?

1 Like

Yep, you got it. The loop checks every name, and once the loop is done, if your function has not returned, you must not have a matching name. Good work getting it passing!

2 Likes

Thanks for your help, I appericiate it very much!

1 Like

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