Gabriele Ferro Home

Intermediate JavaScript Algorithm Scripting on freeCodeCamp

Today I wanted to focus on the freeCodeCamp path for JavaScript Algorithms and Data Structures Certification.

I've skipped the Pig Latin challenge for now because I can't do it.

Day 59: 10 October 2020

**Today’s Progress:**

Today I solved 4 challenges.

My first challenge concerns the manipulation of strings.

Perform a search and replace on the sentence using the arguments provided and return the new sentence.

First argument is the sentence to perform the search and replace on.

Second argument is the word that you will be replacing (before).

Third argument is what you will be replacing the second argument with (after).

Preserve the case of the first character in the original word when you are replacing it. For example if you mean to replace the word "Book" with the word "dog", it should be replaced as "Dog"

Search and Replace - freeCodeCamp
Search and replace

First we make a copy of our input string and write a mini-regex to check whether the letter in question is written in capital letters.

Then we test the regex on the first letter, to verify that the word we are going to replace begins with a capital letter.

If the word starts with a capital letter, the first letter of the word to be replaced will be capitalised. In the opposite case we will have a lower case letter. Using the toUpperCase() and toLowerCase() methods we transform the letter and merge it with the substring we get by excluding the first letter.

Once the first letter matches our needs, we can call the replace function normally.

The second is about arrays and their manipulation to create a nested data structure.

The DNA strand is missing the pairing element. Take each character, get its pair, and return the results as a 2d array.

Base pairs are a pair of AT and CG. Match the missing element to the provided character.

Return the provided character as the first element in each array.

For example, for the input GCG, return [["G", "C"], ["C","G"],["G", "C"]]

The character and its pair are paired up in an array, and all the arrays are grouped into one encapsulating array.

DNA Pairing - freeCodeCamp
DNA Pairing

The first thing to do is, as always, to create a copy of our input according to the Functional Programming approach.

When copying, we convert the string into an array using the split() method, and then create our output variable that will contain the result.

Knowing the length of the array, we check the letter in question with a switch, and based on it, we'll push the desired element into the output variable.

The third challenge always relates to arrays to compare strings, but it tests the ability to manage array indexes.

I probably could have found the solution using other methods, but this was the easiest and most immediate solution for me to implement.

Find the missing letter in the passed letter range and return it.

If all letters are present in the range, return undefined.

Missing letters - freeCodeCamp
Missing Letters

At first sight it seems very easy, almost banal.

Then, I noticed these two tests that had to pass to validate the solution:

  1. fearNotLetter("stvwx") should return "u".
  2. fearNotLetter("bcdf") should return "e".

To do this, we create an array that will contain all the letters of the alphabet.

We create a copy of the input… and while we're at it, save the first letter of our input string in another variable.

Then we check where in our alphabet we "start"; that is, we retrieve the index of the position of the first letter of the input string within the alphabet. This will give us the distance between the beginning of the alphabet (the letter A) and the beginning of the input string.

For each character in our input string, we check if the two letters are different, applying the offset to calculate the two test cases where the input string does not start for letter A.

The first character that will be different is the missing letter.

The fourth challenge is always based on arrays, but this time from a mathematical point of view.

Write a function that takes two or more arrays and returns a new array of unique values in the order of the original provided arrays.

In other words, all values present from all arrays should be included in their original order, but with no duplicates in the final array.

The unique numbers should be sorted by their original order, but the final array should not be sorted in numerical order.

Check the assertion tests for examples:

Sorted Union - freeCodeCamp
  • uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]) should return [1, 3, 2, 5, 4].
  • uniteUnique([1, 2, 3], [5, 2, 1]) should return [1, 2, 3, 5].
  • uniteUnique([1, 2, 3], [5, 2, 1, 4], [2, 1], [6, 7, 8]) should return [1, 2, 3, 5, 4, 6, 7, 8].

The challenge asks us to create an unordered array with non-duplicated elements.

Sorted Union

Honestly, I don't know I should have ignored the arr variable, but I preferred to use the arguments property to retrieve the array of arguments passed as parameters to our function.

Using this information we push the elements towards a new empty array.

We use the flat method to remove all nests.

We create a Set from our array of numbers. By doing so, the output variable will automatically contain unique elements.

I must confess that this challenge was really easy for me because 2 days ago Proful Sadangi ( @profulsadangi on Twitter ) aka "Cheatsheet-Man" posted this image on Twitter.


I had created for the fifth OpenClassRooms project the entry form that would be visible for the mobile viewport. Due to a margin problem the layout broke down, so in a burst of desperation I discarded the changes.

Never do such a thing again.

**Link to tweet:**

[twitter-follow screen_name='20Gabriele20']

July 2024


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