Gabriele Ferro Home

Intermediate Algorithm Scripting Challenges – Part. 2

Some of these challenges are difficult for me, which is why, I needed to review some concepts.

Day 45: 16 September 2020

**Today’s Progress:**

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

Intermediate Algorithm Scripting: Seek and Destroy

Using the Pomodoro Technique I allocated 4 "pomodori".

It means "tomatoes" in Italian and 4 tomatoes are equals to two hours.

Then I started coding... or rather... staring at the screen. After 30 minutes of trial and error I came up with this code.

I tried to write the solution using various techniques but, at the end, I knew that I wanted to use an arrow function and the filter method.

We store the array containing our arguments in a temporary variable using the spread syntax.

Then we use the shift method that removes the first item of our array. Since the method returns the removed item, we store it in a variable for later use.

A particular attention should be paid to the mutation that the shift method implies. Following the Functional Programming approach we must avoid to change the input item, to exclude side effects. That's why we use a temporary variable to store the array of arguments.

Next, we use the filter method to the array using an arrow function. All the elements that pass the test implemented by the provided function are stored in the arr variable.

This variable is finally returned.

The second hour was dedicated to the next challenge.

I don't want to look a the solutions so I prefer to make the necessary effort until a light headache appears.

After the headache appeared, I started looking on the Internet for the documentation. I still can't understand how to properly write the solution.

I found an article about the "Array intersection, difference, and union in ES6" that's really interesting.


I only need to take some rest.

**Link to tweet:**

[twitter-follow screen_name='20Gabriele20']

June 2024


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