Tag: Solutions

FCC Bonfire Series 133: Finders Keepers

Time for a new bonfire it is, this time, we’ll fight our way through Finders Keepers. This bonfire will have us create a function that takes two arguments; The first will be an array, and the second a will be truth test (a function that returns true/false). Our function must return the first item in the array that passes the test.

Free Code Camp hints at the using of the Array.prototype.some() method, and that’s where we are going to be starting, but we will also create a less functional version that, will, once again, be quite faster.

The Array.prototype.some() method will iterate over an array and execute a callback (function) over each item until one of them returns true. Once an item has passed the test, some() will return true; if no item passed, then, it is false. But…

FCC Bonfire Series 132: Smallest Common Multiple

Math goodness for everyone today! We are going to be calculating the Smallest (or Least, or Lowest) Common Multiple of a range of numbers, as prompted by the Smallest Common Multiple bonfire. In mathematics, the least common multiple is the smallest positive number that is a multiple of two or more numbers (source).

As an example, take numbers 5 and 11. What is the smallest number divisible by the two? Let’s get the multiples of each and find out!

  • Multiples of 5: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65…
  • Multiples of 11: 11, 22, 33, 44, 55, 66, 77, 88, 99…

As you can see, the first match we come across is number 55, that number is the least common multiple. We could try and use fancy math formulas to get it, but we are going to take advantage of our dear friend, the processor to take on the workload this time (at first). Today, I shall present you three different ways to achieve the same goal. One of them will be a more readable, short version, the second will be a little messy, but takes about half the time to come up with a solution. And the third will use some clever math and come up with the answer much faster! Interested? Let’s get to it.

FCC Bonfire Series 130: Sum All Odd Fibonacci Numbers

Leonardo Fibonacci. He loves JavaScript!

Oh, good old Fibonacci! Today, we are going to be working with this marvelous integer series. For those not familiar with it, you can check what the Fibonacci sequence is here. For those interested, the guy in the picture (Leonardo Fibonacci) came up with it. He loved math and was from Pisa, Italy.

Let’s do a quick recap. The Fibonacci sequence is formed by summing the two previous numbers to obtain the next. We start off like so:

0, 1, (0 + 1), (1 + (0 + 1)), ((0 + 1) + (1 + (0 + 1))… but we can see it more clearly like so: 0, 1, 1, 2, 3, 5, 8, (8 + 5), etc.

This bonfire will have us sum all odd Fibonacci numbers up to and including the given number (if it is a Fibonacci number). For example:

// Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89...

sumFibs(4) // -> 0 + 1 + 1 + 3 = 5

sumFibs(9) // -> 0 + 1 + 1 + 3 + 5 = 10

And so on. Given this information, the conclusion is obvious: we need a way to identify the three requisites for a number to be added:

  • Is Fibonacci number.
  • Is an odd number.
  • Is lower than or equal to the given number.

Let’s get to coding! We’ll start by getting the Fibonacci numbers up to the given number, once that’s out of the way, the task at hand will become trivial (we would just need to check if it’s an odd number!).

FCC Bonfire Series 129: Spinal Tab Case

Hello campers of young and old, today, we are going to be converting strings to spinal-tab-case. If you did not know about spinal-tab-case, well, you do now. You just-saw-it-twic… thrice!

Spinal case simply removes all spaces and sets dashes instead, while having the string be all lower-case characters. It sounds easy to achieve, we are replace wizards after all; but we’ll see that some cases might not be so straightforward.

First thing that comes to mind is probably:

function spinalCase(str) {
  return str.replace(/ /g, '-').toLowerCase();
}

We first replace all spaces with dashes, and then get rid of capital letters. And you see, that works for strings such as:

spinalCase('Hello world!'); //-> hello-world!

spinalCase('I want to see the world BURN.'); //-> i-want-to-see-the-world-burn.

But how about these other case?

spinalCase('Hey_I_got_you_this_time'); //-> hey_I_got_you_this_time

Let’s get rid of those underscores too!