Tag: operator

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 126: Boo who

We are going to get it done FAST today. This next bonfire, Boo Who will have us write a function that given a value, return true if that value is a boolean primitive (true or false) and false otherwise.

Remember that we are checking for primitive boolean values, not truthy or falsy ones.

Let’s get straight to the action by using the typeof operator. Remember that typeof operates over a value and returns it’s type:

typeof 'hello world';  //-> "string"
typeof 4;  //-> "number"
typeof true;  //-> "boolean"

Let’s start by using a conditional if statement:

function boo(value) {
  if (typeof value === 'boolean') {
    return true;
  } else {
    return false;

Pretty simple isn’t it! Let’s make it a little cleverer (I just found out that cleverer is an actual word).