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
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.
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
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?
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!