How to apply what I learn? How to put the pieces together?

Hi everyone,

What advice do you guys have on how to make the code work together? I’m going through the intermediate algorithm challenges in Javascript and i feel like i know the methods and code to use but i don’t know how to organize them so that it all works together. I’m getting frustrated because i look at the hints and how people solve the problem, and I have some part of the code that are on the right track but they aren’t arranged properly so that it works as a whole.

What is your advice on this? How should i approach each challenge better?

Right now, i do the step by step process. For example, if i want to make things into an array, i know how to do that, then i have to sum it all up, i know how to do that. Or, putting different methods together like map, filter, reduce, some etc, seems difficult to do, but i know how each method works by itself. Its the matter of putting it together that is really frustrating. I do my debugging process by using console.log, and it outputs correctly. When i try to put it all together, its like back to square one and i restart all my code like i don’t know what i’m doing.

Please help.

The fact that you understand the array higher order functions (map/filter/reduce etc) puts you further along in your understanding than you might be judging yourself to be, since you’re getting the basics of functional programming.

A good next step is to start recognizing patterns like the way you’ve mentioned and practice applying them in real world code. Try porting non-functional code to functional idioms, and since you’re experienced with the Array HOFs, start with loops. When you see a for-loop that builds up some output array, you know you have a map. If there’s a continue inside an if statement in the loop, you probably have a filter. If it’s updating some single value per pass, it might be a reduce. Start with easy cases – filter+map is usually pretty easy to recognize – then work your way to more advanced cases and look into libraries like Ramda for them.

BTW, I’ve been programming for 25 years, and I mostly use console.log too.

1 Like

Oh, and if you want some specific challenges, how about these:

  1. Implement the .map() method using .reduce()
  2. Implement the .filter() method using .reduce()
  3. Do the same for 1 and 2 using the .flatMap()method instead of reduce.

reduce and flatMap are ridiculously powerful once you grok how they work.

Thanks for the reply! i’ll definitely try this out!