What's wrong in my code under function?

My code is not working. Kindly inform me what’s wrong in it.

  **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 (var i = 0; i < contacts.length; i++) {
  if (contacts[i].firstName === name) {
    if (contacts[i].hasOwnProperty(prop)) {
      return contacts[i][prop];
    } else {
      return "No such Property";
    }
  }
  return "No such contact";
}
// Only change code above this line
}

lookUpProfile("Akira", "likes");

Challenge: Profile Lookup

Link to the challenge:

In your for loop, you are exiting on the first loop, alway. If the first person doesn’t match, you exit.

Take the first test case:

lookUpProfile("Kristian", "lastName")

It’s like I told you, “Go to that house and find out if there is someone named Kristian who has a last name.” You go over and see that there are 5 people in the house. You ask the first one if their name is Kristian. They say no, so you come back and say that there is no such person in that house.

Remember that return completely exits whatever function it is in.

If the prop is there , it returns the value, if not , it returns the statement mentioned. So, the return is exiting the if condition or the whole lookUpProfile function?

A return always exits a function. They don’t exit if expressions or loops or anything else - I mean, technically they exit them, but only in the process of exiting the function.

But isn’t the i variable supposed to be incremented by 1 if the firstName doesn’t match the name argument? It won’t encounter the return statement right?

Well then it’ll encounter the return statement in the else instead, so same result, the function will exit on the first entry in the array regardless of whether it has the name you’re looking for or not

That else statment is inside the second if condition, what if the first if condition is not met? Then there will be no return statement .

It’s in the loop, the loop runs whatever is in that block every time. And if the if is false, sure, that won’t run, instead it will return as you’ve specified.

for (var i = 0; i < contacts.length; i++) {
  if (THIS IS FALSE) {
    // Then none of the code here runs
  }
  return "No such contact";
}
1 Like

Ohhk , the placement of “No such contact” was wrong that’s y I was getting an error. Thanks for the discussion , it helped me in figuring out the error.

2 Likes

If the contact can’t be found, the function should return the “no contact found message”.

At what point within that function can you know that there isn’t a contact?

Edit: yes :slightly_smiling_face:, you just needed to move that return to a point when the loop was finished (at which point you know for sure that nothing has been found)

1 Like

Actually, i did try it out with the return statement at that position earlier but for some reason it didn’t work out and now it did.
Anyways, Thanks a lot :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.