Higher functions on complex problems frustrate me!

Hi.I am doing the intermediate algorithm scripting challenges on fcc and i find myself stuck when i have to use filter method on more complex problems especially on multiple arrays and objects.Can you recommend me any good tutorials or docs that can make higher functions on complex problems more clear for me? Thanks for your time.

You have the series of MPJ on Higher Order Function or the article here on FCC on the topic that covers map, reduce and filter.

Also if you could be more descriptive on what is it that your having the most problem understanding, people here in the forum might be able to help out :smile:

1 Like

Hi,thanks fro replying.I cant solve the wherefore art thou and sorted union challenges on the intermediate algorithms scripting.There i find myself stuck and frustrating.I dont know what to do.I will check the video you mentioned,tho if you have any extra source for me now that you know what i find hard to solve,i would like it. :slight_smile: Thanks,again!

Since I can’t recall any more material to send, let me attempt to help by describing my thought process while solving this kind of problem.

Let’s take Wherefore art thou as an example:

Since we have multiple tests it’s better to start solving each one at a time since each represents a different problem.

We know our function receives an array called collection that we will have to filter with the data that is in source so something like collection.filter should be a good start.

arr = collection.filter(eachElementOfTheArray => {
  // return some value

As for what to put inside the filter well, we know that we have to match the source property last with eachElementOfTheArray property last.

So, to pass the first test we should have something like:

arr = collection.filter(eachElementOfTheArray => {
  return eachElementOfTheArray.last === source.last

This actually solves the first two tests so you just solved 2 out of 6 problems, progress! This tells us that our filter is most likely correct so whatever change we need to do from this point onward is in the return inside of the filter.

Now, for the 3rd test onward we no longer have only one property last, we have to check multiple properties.

One way to do this part would be to iterate through the objects keys of our source and check if every property matches our eachElementOfTheArray properties.

Using those keywords you can complete the test and should still be challenging enough to put it together or come up with a different solution that best fits you.

Also, whenever you feel stuck and frustrated decompose the problem and take a break from it, it helps to come back later with a clear mind and to simpler tasks to complete instead of a big one.

I hope it helps :smile:

Anything that can be done with high order methods can also be done with basic loops. One approach is to solve the problem with basic loops and then refactor your code to use high order methods.

The important thing is the logic. Once you have the logic to solve the problem, you can then improve how you express that logic.

1 Like