Understand the Fibonacci Sequence in javascript

Understand the Fibonacci Sequence in javascript

Learn the Fibonacci series in JavaScript and understand how to use it in the real world.

Welcome to this tutorial, in this tutorial, we discuss how to use the Fibonacci Sequence in the real world. Be informed that the Fibonacci series is used in design, programming and trading; It is used as a trading strategy called the "Fibonacci Retracement Strategy". Now the question is what is Fibonacci Sequence?

What is fibonacci sequnece?

Click the image below to watch its video version:Fibonacci series in Javascript

Fibonacci sequence is the sequence in which every number is the sum of the previous two numbers. This is what I mean:

0, 1, 1, 2, 3, 5, 8, 13, 21

The Fibonacci sequence can be very useful in the real world, especially in design and computer science. We will come back to that later; In this article, let’s focus on converting the Fibonacci sequence to code.

Fibonacci sequence with Mathematics notation:

Fn = Fn-1 + Fn-2

with the seed values F0 = 0 and F1 = 1;

This is what the formula means: If you look at the Fibonacci sequence, you will realize a number is the sum of the two previous numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21

Find the Value of the nth term of the Fibonacci sequence with JavaScript.

const getFibonacciValueForTerm = number => {

  if(number === 0) return 0;

  let latestTerm = 1;
  let previousLatestTerm = 0;
  let nTerm = number;

  for(let index = 2; index <= number; index++) {
    // calculate the value of the current the term in Fibonacci sequence
    nTerm = latestTerm + previousLatestTerm;

    //update previous terms for the next operation
    previousLatestTerm = latestTerm;
    latestTerm = nTerm; 
  }

  return latestTerm;
};    

const seventhTerm = getFibonacciValueForTerm(7)
console.log(seventhTerm)

Let's explain the Fibonacci sequence function above step by step:

Step 1: Set Fabonacci seed values

 if(number === 0) return 0;

  let latestTerm = 1;
  let previousLatestTerm = 0;
  let nTerm = number;

First, we return 0 if 0 is passed into the function. Then, you have to recall that the formula to calculate the nth term of the Fibonacci sequence is:

Fn = Fn-1 + Fn-2

The information below explains everything in English:

Fn = nTerm;

Fn-1 = latestTerm

Fn-2 = previesLatestTerm // previous to Fn - 1

nTerm = latestTerm + previesLatestTerm;

Step 2: Calculate the value of the nth term repetitively

This part is to keep calculating the nth term of the Fibonacci series until will get to the given nth term. For example, if we are given 6th, then the function will start calculating nth from the first term until it gets to the 6th term.

  for(let index = 2; index <= number; index++) {
    // calculate the value of the current term in Fibonacci sequence
    nTerm = latestTerm + previousLatestTerm;

    //update previous terms for the next operation
    previousLatestTerm = latestTerm;
    latestTerm = nTerm; 
  }

We use a loop as we are repeating the same process to calculate from the first term to the given term.

Sub-step 1: Calculate the nth term

  // calculate the value of the current term in the Fibonacci sequence
  nTerm = latestTerm + previousLatestTerm;

Sub-step 2: Update Fibonacci seed values

As we are calculating repeatedly from the first term to the given nth term, we need different seed values for each of the terms. For example, we need the value of the first and the second terms to calculate the third term and we need the third and second terms to calculate the fourth term.

So we need to keep updating the seed values for the next operation and the function below does just that.

  //update previous terms for the next operation
  previousLatestTerm = latestTerm;
  latestTerm = nTerm;

Step 3: Return the value of the given term

return latestTerm;

Final step: Apply the function

const seventhTerm = getFibonacciValueForTerm (7)
console.log(seventhTerm)

Calculate Fibonacci sequence using Recursion

In this part, we are going to calculate the Fibonacci sequence with Recursion. The function below does that:

const getFibonacciValueForTerm = (number) => {

  if( number < 2) {
    return number;
  }

  return getFibonacciValueForTerm(number - 1) + getFibonacciValueForTerm(number - 2);
}

console.log(getFibonacciValueForTerm(3))

If you don't understand recursion, click the video below to learn about Recursion:

How to Understand Factorial Program in JavaScript

fibonacci sequence image.PNG

The image above explains the step by step of how recursion achieves the Fibonacci sequence.

Conclusion

The fibonacci sequence is one of the general algorithms in programming. It is used in several fields such as design, computer science and even trading. As you have seen earlier in this tutorial, it can be used to build the stock trading strategy called Fibonacci Retracement Strategy. Now, go and implement everything you have learnt here. Don't forget, "A true Roman, never surrenders". See you soon.