Tell us what’s happening:
Describe your issue in detail here.
Please help why is this not working. is it because i used && in first condition because that logic doesnt seem false. Please 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++){
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)){
return contacts[i][prop];
}
if(!contacts[i].hasOwnProperty(prop)){
return "No such property";
}
}
return "No such contact";
}
// Only change code above this line
console.log(lookUpProfile("Bob", "potato"));
**Your browser information:**
User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.
return doesn’t exit the loop. It completely exits the function. So, once we hit this, it will never check any other record. If you notice, in the example I gave you above, you only hit the first record:
You need to dig in and figure out exactly what it is doing and why. Why is it failing after the first record? After only checking one record, how can it know that there are no records with that property? That is an error in your logic.
I am trying to help you learn how to debug your own code. This is a fundamental skill for a developer. Don’t just stare at the code - test things. Put in log statements, test things. Compare how things are behaving to how they should behave.
Have you thought about the algorithm? Maybe stop thinking about JS for a minute. If you had to explain this to a child, how to work through these records and check them, what would you tell them? Can you write that out in pseudo-code?