Please Help! I do not whats wrong in my code here

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

Challenge: Profile Lookup

Link to the challenge:

You need to work on your logic a bit. You have the right basic idea, but you need to think when you are returning.

When in doubt, log it out.

When I add these logs to your code:

  // ...
  for (let i = 0; i < contacts.length; i++){
    console.log(name, prop)
    console.log(contacts[i])
  // ...

and feed it this:

console.log(lookUpProfile("Bob", "potato"));

I see this in the console:

Bob potato
{ firstName: 'Akira',
  lastName: 'Laine',
  number: '0543236543',
  likes: [ 'Pizza', 'Coding', 'Brownie Points' ] }
No such property

Why is it failing on the first record? Why is it saying “No such property”? Shouldn’t it be saying “No such contact”?

You need to examine your logic. Put it some more log statements if you need them.

Sorry Sir but i am not able to figure it out

You don’t understand why it is returning that? What part is confusing you?

for (let i = 0; i < contacts.length; i++)
  1. I am iterating through array using for
if (contacts[i].firstName === name && contacts[i].hasOwnProperty(prop)){
  return contacts[i][prop];
  }
  1. looking for conditions where first name property is same as asked and checking prop property is there , if so return the said property
if(!contacts[i].hasOwnProperty(prop)){
  return "No such property";
  1. if contacts doesnt have said property return that line

  2. otherwise return No such contact

i got that if i used 2nd points conditionals seprately it would work but why doesnt it work using &&

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.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

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?