# Profile Lookup: Why isn't this code working?

**URL:** <https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563>\
**Category:** General\
**Created:** [January 14, 2017, 4:51pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563 "2017-01-14T16:51:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![anadebarros](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anadebarros/32/32203_2.png) [@anadebarros](https://forum.freecodecamp.org/u/anadebarros)\
**Post date:** [January 14, 2017, 4:51pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/1 "2017-01-14T16:51:36Z")

</div>

I cannot understand why my code does not work. Could someone explain it? (it says: “Bob”, “number” should return “No such contact”). I saw a lot of answers here which worked but I do not understand why this one is wrong…Thanks.

```javascript
//Setup
var 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(firstName, prop){
// Only change code below this line
 
  for(var i=0; i<contacts.length; i++){
    
      if(firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
        return contacts[i][prop];
      } else if (!contacts[i].firstName){
        return "No such contact";
      } else if( !contacts[i].hasOwnProperty(prop)){
        return "No such property";
      }
        
      
    }
  

// Only change code above this line
}

// Change these values to test your function
lookUpProfile("Sherlock", "lastName");

```

---

<div class="post-metadata">

**Author:** ![IsaacAbrahamson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/isaacabrahamson/32/30882_2.png) [@IsaacAbrahamson](https://forum.freecodecamp.org/u/IsaacAbrahamson)\
**Post date:** [January 14, 2017, 4:53pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/2 "2017-01-14T16:53:27Z")

</div>

I cleaned up your code.  
You need to use triple backticks to post code to the forum.  
See [this post for details](http://forum.freecodecamp.com/t/forum-code-formatting/25574).

---

<div class="post-metadata">

**Author:** ![anadebarros](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anadebarros/32/32203_2.png) [@anadebarros](https://forum.freecodecamp.org/u/anadebarros)\
**Post date:** [January 14, 2017, 4:58pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/3 "2017-01-14T16:58:33Z")

</div>

Oh…didn’t know that, thanks!

---

<div class="post-metadata">

**Author:** ![IsaacAbrahamson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/isaacabrahamson/32/30882_2.png) [@IsaacAbrahamson](https://forum.freecodecamp.org/u/IsaacAbrahamson)\
**Post date:** [January 14, 2017, 5:37pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/4 "2017-01-14T17:37:24Z")

</div>

As for an explanation, the reason “No such contact” is never returned, is the else statement is never reached:

```javascript
if(firstName == contacts[i].firstName && contacts[i].hasOwnProperty(prop)){
  // works semi-correctly
  return contacts[i][prop];
} else if (!contacts[i].firstName){
  //never reached
  return "No such contact";
} else if( !contacts[i].hasOwnProperty(prop)){
  // works correctly
  return "No such property";
}

```

Let’s look a little deeper as to your if statement logic. Whenever something like this happens, its always a good idea to debug with different test cases.

For example, try this out:

```javascript
lookUpProfile("Bob", "bruh");
// ==> 'No such property'

```

```javascript
lookUpProfile("Bob", "firstName");
// ==> undefined

```

```javascript
lookUpProfile("Akira", "number");
// ==> '0543236543'

```

There is clearly an error in your `if` logic. Just from the previous test cases, I can see three things:

1. A perfect search works correctly
2. A search with an invalid firstName but valid property returns `undefined`
3. A search with an invalid firstName and invalid property returns `'No such prop'` instead of `'No such contact'`

Ok, so what is going on here? When I do:

```javascript
lookUpProfile("Bob", "bruh");

```

Your code does this:

```javascript
if("Bob" == "Akira" && "Akira".hasOwnProperty("bruh") ){

```

So, in this case, it should move on to the next because Bob is not Akira and Akira does not have bruh:

```javascript
else if (! "Akira")

```

Oops, Akira does exists. So now when we go to this:

```javascript
else if( ! { firstName: 'Akira',
  lastName: 'Laine',
  number: '0543236543',
  likes: ['Pizza', 'Coding', 'Brownie Points'] }.hasOwnProperty("bruh"))

```

_!!! ERROR !!!_ it passes. The code sees that Akira does not have a property bruh and sends the message, but it should have stopped earlier and noticed that no object in contacts has the firstName of Bob. The code never says that Bob is not a property, because it takes the false property and checks it to Akira.

Now the next case.

```javascript
lookUpProfile("Bob", "firstName");

```

returns undefined. Why?

```javascript
if("Bob" == "Akira" && "Akira".hasOwnProperty("firstName"))

```

Not going to pass here. Bob does not equal Akira

```javascript
else if (! "Akira")

```

Nope. Akira is not false. The code never sees that Bob is false either.

```javascript
else if( ! { firstName: 'Akira',
  lastName: 'Laine',
  number: '0543236543',
  likes: ['Pizza', 'Coding', 'Brownie Points'] }.hasOwnProperty("firstName"))

```

Oops. Akira does have a first name (although its driven my crazy typing it the last 20 minutes… 😖)  
So Bob.firstName goes all the way through the tests without passing any. Thus it is returned from the function undefined because no return statement was specified outside of the else if statements.

I hope you see the errant logic. The code is close, and with a couple tweaks, maybe nesting the property check in the firstname check ;), you should get it to work. Happy Coding 😃

---

<div class="post-metadata">

**Author:** ![anadebarros](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/anadebarros/32/32203_2.png) [@anadebarros](https://forum.freecodecamp.org/u/anadebarros)\
**Post date:** [January 14, 2017, 6:00pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/5 "2017-01-14T18:00:43Z")

</div>

Thank you very much for your explanation! I looked at it now and it made sense…JS can be quite tricky…😕  
Thank you!!

---

<div class="post-metadata">

**Author:** ![IsaacAbrahamson](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/isaacabrahamson/32/30882_2.png) [@IsaacAbrahamson](https://forum.freecodecamp.org/u/IsaacAbrahamson)\
**Post date:** [January 14, 2017, 6:05pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/6 "2017-01-14T18:05:28Z")

</div>

> [@anadebarros](#):
>
> JS can be quite tricky…😕

Don’t blame JS. Any language can be tricky, especially in complex logic statements. It would not have been any easier in C#, Java, etc. The more you do it, the better you will become.

---

<div class="post-metadata">

**Author:** ![meerkat79](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/meerkat79/32/161483_2.png) [@meerkat79](https://forum.freecodecamp.org/u/meerkat79)\
**Post date:** [August 17, 2017, 10:43am UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/7 "2017-08-17T10:43:37Z")

</div>

Thank you @anadebarros for your question. When I looked at this question the first time I went down exactly the same road as you! @IsaacAbrahamson thank you for your reply it was very insightful however I would like to point out a few things to clarify as I don’t feel the answer got to the crux of the matter.

Firstly I would like to point out: What we really want to use when iterating over objects in an array is a forEach loop. On some occasions a for loop is preferred however and this is such a case.  
I think the most salient feature we need to realise about the forEach loop is that it is going to go over each object even once our lookUpProfile function has returned the desired result we were looking for. The for loop will not. It will break out of the loop the moment any of the conditions are met.

This raises the second point: In our example from Isaac where we run lookUpProfile(“Bob”, “bruh”) for example, it does not return undefined. It actually returns ‘no such contact’ and correctly so. So what does happen then?

The first if statement is evaluated and it is found that the firstName of the first object! This part is important… Does not match the provided Bob as provided in the arguments. It thus returns the result…and…wait for it…exits the for loop!  
Nothing else happens after this point and none of the other firstName’s in the other objects are checked against the argument provided.

It is only when we nest additional conditions within each other that the loop exists at the point where we want it to as we find in the working example. We ‘solve’ the same problem with a forEach loop however we will then find we are returned all the messages for each (pun intended) of the objects including the desired result.

I hope this helps. I know the context of the problem was only with for loops but I feel it is only when considering the forEach loop and doing a comparison that one gets the deeper lesson 😃

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 2:47pm UTC](https://forum.freecodecamp.org/t/profile-lookup-why-isnt-this-code-working/76563/8 "2021-01-16T14:47:37Z")

</div>

Closed on January 16, 2021, 2:47pm UTC


