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
https://www.freecodecamp.org/learn/full-stack-developer/lab-profile-lookup/lab-profile-lookup