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"));
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
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
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
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.