Wherefore art thouu

Any idea why this doesn’t work ?

function whatIsInAName(collection, source) {
  // What's in a name?
  var prop = Object.keys(source);
  
  return collection.filter(function(val) {
    for (i = 0; i < prop.length; i++) {
      if (val.hasOwnProperty(prop[i]) == 1 && val[prop[i]] === source[prop[i]]) {
        return true;
      }
      else {
        return false;
      }
    }
  });
}

whatIsInAName([{ "a": 1, "b": 2 }, { "a": 1 }, { "a": 1, "b": 2, "c": 2 }], { "a": 1, "c": 2 });

Ok I try this way and it work (that was the way they did in the solution, I’ve look at it, wait a night and try to do it by myself but mix it up …)

I did two mistakes, I think I didn’t understand the filter function, I’m not at ease with javascript functions.

So I shall return false first so that when he returns true at the end he doesn’t take the false value in account ?

I put my false return in the loop because I was thinking the filter function will remember when it’s true and when it’s not and at the end return collection.filter with only the true value. I’m not sure I really understand the computer logic.

Proof of that I’m not even able to clearly explain what I don’t understand

Ok nice explaination here, very clear.

So now I know that filter() take a function in parameter and this function could be any function.
I use to think you always need to declare a “pseudo function” in the .filter(function(val) { … }); and tend to think this function can only be access by the filter method on the array you pass.

So filter can just take any function you want, and the function could be written anywhere in the file, it’s just a different way of organizing the code it assume it will return either true or false and loop through an array, send each case to the function and display it

Thanks a lot because it’s much more easy to understand like that. I’m trying to keep doing a bit of freecodecamp every day (i don’t have much free time to do it but I try to stay regular) and at the beginning I found that easy because each challenge bring a little something new but the gap was small between each. Now it’s like each challenge is suppose to take more time to solve and do research to find out about how javascript work and the documentation is often chinese to me, go to get use to it.

If you know a greate youtube channel or mooc that give a bit more accessible information don’t hesitate to share it :slight_smile:

Thanks a lot !

Hey sorry I didn’t see your answer because I only log on the forum when I don’t manage to do my daily challenge alone, thanks mate I’m going to check Fun fun function and The Code Train :slight_smile: