# Profile\_Lookup Help

**URL:** <https://forum.freecodecamp.org/t/profile-lookup-help/497834>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 20, 2022, 4:56pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834 "2022-02-20T16:56:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lbrod](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lbrod/32/249902_2.png) [@lbrod](https://forum.freecodecamp.org/u/lbrod)\
**Post date:** [February 20, 2022, 4:56pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/1 "2022-02-20T16:56:32Z")

</div>

Can someone help me understand why this solution doesn’t work for the Profile Lookup:

```auto
for (let i = 0; i < contacts.length; i++){
    if (name == contacts[i].firstName && contacts[i].hasOwnProperty(prop) == true){
      return contacts[i][prop];
    } else {
      return "No such property";
  }
  return "No such contact";
}

```

Do we have to use a nested if statement, or can we use the “and” statement like I did?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 20, 2022, 4:59pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/2 "2022-02-20T16:59:37Z")

</div>

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.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 20, 2022, 5:01pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/3 "2022-02-20T17:01:28Z")

</div>

It is best to use the **Ask for Help** button so we get a link to the challenge.

A return statement immediately stops a function. Also, the logic of your else isn’t quite right. That else will always run when the if condition is not true.

---

<div class="post-metadata">

**Author:** ![lbrod](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lbrod/32/249902_2.png) [@lbrod](https://forum.freecodecamp.org/u/lbrod)\
**Post date:** [February 20, 2022, 6:54pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/6 "2022-02-20T18:54:30Z")

</div>

If the return statement stops a function, doesn’t that mean we would need to create another function in order to get both statements?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 20, 2022, 6:58pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/7 "2022-02-20T18:58:26Z")

</div>

Not at all… You need the return statements in the correct places guarded by the right conditional logic.

---

<div class="post-metadata">

**Author:** ![lbrod](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lbrod/32/249902_2.png) [@lbrod](https://forum.freecodecamp.org/u/lbrod)\
**Post date:** [February 20, 2022, 8:24pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/8 "2022-02-20T20:24:25Z")

</div>

What do you mean by “stops”? I’m not understanding the importance.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 20, 2022, 8:25pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/9 "2022-02-20T20:25:41Z")

</div>

If your function returns inside of a lop, the loop doesn’t do any more iterations. The function immediately returns the value.

---

<div class="post-metadata">

**Author:** ![lbrod](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lbrod/32/249902_2.png) [@lbrod](https://forum.freecodecamp.org/u/lbrod)\
**Post date:** [February 20, 2022, 8:46pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/10 "2022-02-20T20:46:29Z")

</div>

Isn’t that true of the posted solution as well? The only difference is that I’m using an and value instead of a nested if.

With either “and” or a nested if, if we hit get a value of i that matches the conditions, the loop stops and we get taken out of the function, right? I’m having trouble seeing what’s changing between the two.

---

<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:** [February 20, 2022, 8:48pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/11 "2022-02-20T20:48:10Z")

</div>

In your case if the name doesn’t match, the else statement is executed, which is not when you want the else statement to execute, is it?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 20, 2022, 8:52pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/12 "2022-02-20T20:52:44Z")

</div>

It’s OK to return early if you return early in the right places. You are bailing out of the function too early in the case where you say no matching contact exists. You can’t say that no contact matches _inside_ the loop that checks all contacts.

---

<div class="post-metadata">

**Author:** ![lbrod](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lbrod/32/249902_2.png) [@lbrod](https://forum.freecodecamp.org/u/lbrod)\
**Post date:** [February 20, 2022, 9:06pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/13 "2022-02-20T21:06:41Z")

</div>

Oh ok. I see, the nested if allows me to check if there is a contact in the whole list of contacts with the given name before then seeing if that contact (assuming it exists) shares the given property.

By contrast, what I was doing was just checking whether the first contact in list had both the given name and property, and if it didn’t have both, I was returning “No such contact.”

---

<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:** [February 21, 2022, 8:20am UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/14 "2022-02-21T08:20:34Z")

</div>

no, for the structure of your if/else the no such contact is never executed, it always stops at no such property. check your function output

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 22, 2022, 8:21pm UTC](https://forum.freecodecamp.org/t/profile-lookup-help/497834/15 "2022-08-22T20:21:12Z")

</div>

Closed on August 22, 2022, 8:21pm UTC

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