0

So I'm working on a FreeCodeCamp javascript problem. I was able to write a working function using a for loop, but when I try to write the same function with a forEach loop, it doesn't work. How can I get it to work?

First, the working function:

function lookUpProfile(name, prop){
    for(var i = 0; i < contacts.length; i++){
        if(name === contacts[i]["firstName"]){
            if(contacts[i].hasOwnProperty(prop)){
                return contacts[i][prop];
            }
            return "No such property";
        }
    }
    return "No such contact";
}

The non working forEach function:

function lookUpProfile(name, prop){
    contacts.forEach(function(contact){
        if(name === contact["firstName"]){
            if(contact.hasOwnProperty(prop)){
                return contact[prop];
            }
            return "No such property";
        }
    });
    return "No such contact";
}

The array that is being looped through:

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"]
}

];

The problem guidelines:

  • We have an array of objects representing different people in our contacts lists.
  • A lookUpProfile function that takes name and a property (prop) as arguments has been pre-written for you.
  • The function should check if name is an actual contact's firstName
    and the given property (prop) is a property of that contact.
  • If both are true, then return the "value" of that property.

  • If name does not correspond to any contacts then return "No such
    contact".

  • If prop does not correspond to any valid properties of a contact
    found to match name then return "No such property".

  • You can't, and you shouldn't, use `forEach`. If you're looking for a better looping syntax, go `for … of`. – Bergi Jan 23 '20 at 21:14
  • 1
    When you `return` from a `for()` loop you return from the parent. When you return from a callback function like the one you pass to `forEach`, you return from that callback. – coreyward Jan 23 '20 at 21:15
  • Welcome to Stack Overflow. You can just use `.filter()` or a `.reduce()`, where you can return a value – Mikkel Jan 23 '20 at 21:15
  • 2
    @Mikkel `map` makes no sense here. At best, use `find`. – Bergi Jan 23 '20 at 21:16
  • @Bergi Yep, my bad, I edited my comment – Mikkel Jan 23 '20 at 21:17
  • @user1538301 I still [recommend avoiding `forEach`](https://stackoverflow.com/a/49420944/1048572) in general, which helps avoiding problems like this here. – Bergi Jan 23 '20 at 21:20
  • @Bergi that answer is 25% subjective (readability) and 25% arbitary (the const case), I will give you that a `for` loop is typically more performant (no closure) and you can use control statements. The only time I ever use `for` is when I absolutely need to interrupt the loop at some point. I still think to say that one "can't" and "shouldn't" use `forEach` is a bit extreme – gabriel.hayes Jan 23 '20 at 21:30
  • I see. Thanks for the quick responses everyone. – K Strong Jan 23 '20 at 21:31
  • 1
    The answer is use `find()` not forEach since forEach is designed to loop over everything. Other methods like every() and some() are also overlooked when people want to short circuit for each. – epascarello Jan 23 '20 at 21:37
  • 1
    @user1538301 The "can't" was specific to what the OP was trying to do here, where he needs control flow statements. – Bergi Jan 23 '20 at 21:41

0 Answers0