Profile Lookup Challenge: forEach loop not working

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 :slight_smile:

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:
https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup/

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/profile-lookup

My Solution:

//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.

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.

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");
1 Like

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

1 Like