# Profile Lookup Challenge: forEach loop not working

**URL:** <https://forum.freecodecamp.org/t/profile-lookup-challenge-foreach-loop-not-working/317633>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [September 27, 2019, 12:46am UTC](https://forum.freecodecamp.org/t/profile-lookup-challenge-foreach-loop-not-working/317633 "2019-09-27T00:46:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewEyesman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andreweyesman/32/129777_2.png) [@AndrewEyesman](https://forum.freecodecamp.org/u/AndrewEyesman)\
**Post date:** [September 27, 2019, 12:46am UTC](https://forum.freecodecamp.org/t/profile-lookup-challenge-foreach-loop-not-working/317633/1 "2019-09-27T00:46:20Z")

</div>

**Tell us what’s happening:**  
Hello, I’m trying to solve this coding challenge and I don’t understand why this isn’t working. Everything in my code is identical to the official solution, but I chose to use a forEach loop instead of a regular for loop. Can anyone explain what I’m doing wrong here? Would be much appreciated 🙂

To be more specific, the following code only returns: ‘No such contact’…

I thought maybe it’s not reading cur[prop] so I logged it to the console and it works just fine. So I should be able to return cur[prop] right?

Official Solution:

> **[freeCodeCamp Challenge Guide: Profile Lookup](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-profile-lookup/18259)**
>
> Profile Lookup Problem Explanation Change the code below // Only change code below this line and up to // Only change code above this line. Ensure that you are editing the inside of the lookUpProfile() function. This function includes two...

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup)**
>
> Learn to Code — For Free

My Solution:

```js
//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(name, prop){
  // Only change code below this line
  
  contacts.forEach(cur => {
      if (cur.firstName === name) {
          if (cur.hasOwnProperty(prop)) {
              return cur[prop];
          }
          return 'No such property';   
      }
  })

  return 'No such contact';
  // Only change code above this line
  }
  
  // Change these values to test your function
  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/77.0.3865.90 Safari/537.36`.

---

<div class="post-metadata">

**Author:** ![M-Michelini](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/m-michelini/32/109586_2.png) [@M-Michelini](https://forum.freecodecamp.org/u/M-Michelini)\
**Post date:** [September 27, 2019, 1:16am UTC](https://forum.freecodecamp.org/t/profile-lookup-challenge-foreach-loop-not-working/317633/2 "2019-09-27T01:16:10Z")

</div>

```auto
contacts.forEach(cur => {
      if (cur.firstName === name) {
          if (cur.hasOwnProperty(prop)) {
              return cur[prop];
          }
          return 'No such property';   
      }
  })

```

the return statements inside of the forEach callback dont return in the parent function, unlike a normal for loop returning inside a forEach loop effectively just tells the loop to go to the next item.

you’ll have to return cur[prop] outside of the forEach loop.

```auto
function lookUpProfile(name, prop){
  // Only change code below this line
  let output = null;
  contacts.forEach(cur => {
      if (cur.firstName === name) {
          if (cur.hasOwnProperty(prop)) {
              output = cur[prop];
          }
          else {output = 'No such property';}
      }
  })
if (output) return output;
return 'No such contact'
}
// Change these values to test your function
lookUpProfile("Akira", "likes");

```

---

<div class="post-metadata">

**Author:** ![AndrewEyesman](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andreweyesman/32/129777_2.png) [@AndrewEyesman](https://forum.freecodecamp.org/u/AndrewEyesman)\
**Post date:** [September 27, 2019, 1:22am UTC](https://forum.freecodecamp.org/t/profile-lookup-challenge-foreach-loop-not-working/317633/3 "2019-09-27T01:22:49Z")

</div>

Wow, Thank you for the detailed response! I didn’t know returning items in forEach loops worked like that.

---

<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 25, 2021, 12:15pm UTC](https://forum.freecodecamp.org/t/profile-lookup-challenge-foreach-loop-not-working/317633/4 "2021-01-25T12:15:43Z")

</div>

Closed on January 25, 2021, 12:15pm UTC


