Gabriele Ferro Home

Functional Programming - Part. 3

Day 43: 14 September 2020

**Today’s Progress:**

Completed the Functional Programming challenges on freeCodeCamp for the JavaScript Algorithms and Data Structures Certification.

I finally understood how to properly write arrow functions!

...Et voila!

(I will not publish all the solutions because some of the challenges have been easy and effortless for me.)

Use reduce to analyze data

This function takes an array of movies. Then it creates a variable to hold the result. Finally we can chain all the operation that we need to filter our array. Using arrow functions we can write shorter code and most important less typos and mistakes.

The filter method extracts only the movies with that particular director.

function returnNolanMovies( arrayOfMovies ) {
  let temporaryArray = [];
  for ( let i = 0; i < arrayOfMovies.length; i++ ) {
    if ( arrayOfMovies.Director === "Christopher Nolan" ) {
      temporaryArray.push( arrayOfMovies.Director );
  return temporaryArray;

This piece of code should be (more of less) the equivalent of the following arrow function:

movie => movie.Director === "Christopher Nolan"

Then, thanks to the map method we convert the rating for every movie into a number (ratings were stored as strings).

Finally, we use the reduce method. It is the most general purpose method available to manipulate arrays. We can do EVERYTHING with this method.

Here we use it to calculate the sum of the rating for every movie and then we divide the total for the number of movies from that particular director.

Square only integers

First we filter our array keeping only positive numbers, and then, we discard every floating point number.

Using the map method we apply the multiplication.

Use concat to avoid mutations

Following the Functional Programming approach we must avoid mutations like the plague. They lead to side-effects and headaches. In this case the challenge shows that using concat we are actually creating a copy of our input array. For this reason, the sort method will not modify our original globalArray.

Use split to filter a string and convert into an array

We can write a RegEx (I HATE THEM) to remove unwanted character.

JOIN THE DARK S... split

The join method does exactly the opposite. It converts an array into a string.

May the force be with you.

I love my job.

Winter is coming.

Convert Strings to URL Slugs


This blog makes me sick. I often forget to post my progress and to rewrite my notes (I use a LOT of bloc notes).

It is easy for me to code every day for the #100DaysOfCode challenge but, it is so difficult for me to write every day something "interesting" to share and that is why lately I have posted every two days.

If you are still reading and you want to know how I create these images with the code, I use carbon to create the images and then to reduce the images sizes... by a LOT.

Soon I will create another section in this blog with all the resources that are actually saved into my wakelet list.

**Link to tweet:**

[twitter-follow screen_name='20Gabriele20']

July 2024


Gabriele Ferro Home
Copyright © Ferro Gabriele. All rights reserved.