Lookup profile question - for the life of me, I can't understand why it's not working

For some reason, the below code is not passing tests. What am I missing?

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)){
        console.log(contacts[i][prop])
        return contacts[i][prop]
      }
      else if(contacts[i].firstName!==name){
        console.log("here1")

        return "No such contact"

      }
      else if(contacts[i].hasOwnProperty(prop)==false){
        console.log("here2")

        return "No such property"
      }
    }
    
  }
  
  lookUpProfile("Sherlock", "likes")
1 Like

gokcegursel, hi there!)

Before you write any code, you need to think about its logic. You see, in the instruction it says to check the whole contacts array before outputting “No such contact” or “No such property”. In your code, it checks one object at a time.

Hope that helps, have a great day)

But again, it outputs “No such contact”. Why is that? Is it because the first contact name is not Sherlock?

1 Like

Great question. You can check Python Tutor website to visualize your code and see where the mistake is.

1 Like

I’ve completed this task. It took me a while, but there’s a solution. You just need to use the “filter()” function to check for name and prop. Do it separately, not in one go. If everything’s okay, use the for loop and your first “if” condition to output the result.

Use of filter is in no way required.


The important thing to note is that return immediately stops your function. You should only return this phrase when you are absolutely sure every contact does not match.

2 Likes

I see. Is there any way I can improve how I structure my code logic? I’m guessing that the answer is more exercises :slight_smile:

First off, were you able to solve the problem?


One tip I have is that checking the exceptions first usually cleans up your code.

For example in your original post’s if/else chain, you’re making the same checks twice.

      if(contacts[i]["firstName"]===name&&contacts[i].hasOwnProperty(prop)){
        console.log(contacts[i][prop])
        return contacts[i][prop]
      }
      // Checking the name a second time
      else if(contacts[i].firstName!==name){
        console.log("here1")

        return "No such contact"

      }
      // Checking the prop a second time
      else if(contacts[i].hasOwnProperty(prop)==false){
        console.log("here2")

        return "No such property"
      }

To remove those redundant checks, you could rewrite it in this order:

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

// Since we got past the above checks,
// we know the contact is a match and
// the prop exists, so no need to check again.
return contacts[i][prop];

(to be clear, I’m aware that the above is not the solution to the problem, I’m just using your original code as an example)

1 Like

There is one big thing that you can do. The loop represents checking all contacts. So where, relative to the loop, should the ‘No such contact’ message go? The options are before, after, or inside.

1 Like

Yes, I was able to solve the problem. From what I can gather is that my function was incorrectly constructed (by me :frowning: ) So the first name is “Akira” in the contacts and I was passing “Sherlock” to the function. At i=0 the firstName is Akira, and since they are not equal, my function passes the first if and proceeds to the second if.

The big hint is as the answerers suggested to check all names before returning a value for me.

It should be after, when no contacts with the same firstName is found.
My questions was more general though. I write Groovy scripts for my job regularly and I didn’t expect from myself to make such a mistake honestly. So my question is how can I improve in general the way I construct the logic of my code for any coding problem?

The only way to get better is practice. No shortcut, only practice.

:slight_smile: Thanks by the way. Take care.

1 Like