Can any one tell me why this code doesn't work?

Tell us what’s happening:
Describe your issue in detail here.

  **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) === true)
  {
    return contacts[i].prop
  }

else if(contacts[i].firstName != name)
 {
   return "No such contact";
 } 

 else if( contacts[i].hasOwnProperty(prop) === false)
 {
   return "No such property"
 }

}

// 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/98.0.4758.102 Safari/537.36

Challenge: Profile Lookup

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

1 Like

hello;
It’s the first time I post here so I don’t know very much , can you please tell me how can I post in “tell us what’s happening” section.

You ‘tell us what is happening’ in your code and what you need help with.

What is it doing?

What should it be doing?

That sort of thing.

okay;
I’m asking about the first statement in the function which should return the value of property but when i run the code it doesn’t work.
P.S: I ordered statements again by putting the the second if statement as last one to be logically ordered but the code doesn’t work yet :"(

Your instinct that there is something wrong with the order of your code is correct.

A return statement immediately stops your code. Can you immediately stop and say that you have no matching name inside of a loop checking all names?

1 Like

Think of the loop as a checklist or row call.

You can’t stop reading the list (return) after the first name that doesn’t match what you are looking for. If you do find the name and it doesn’t have the property then you can return. Or at the end if you finish the list and didn’t find the name you can return.

2 Likes

thank you so much It runs :pray:t2:

1 Like

thank you so much it runs

1 Like