Javascript
Mamta Kumawat  

Understanding Fibonacci Sequence in JavaScript

The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. It starts with 0 and 1, and continues indefinitely. Mathematically, the series is represented as:

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

In this blog post, we will walk through a simple JavaScript code to generate the Fibonacci series up to the nth term and explain how the code works.


JavaScript Code for Fibonacci Series

Here’s the JavaScript function to generate the Fibonacci series:

function fibonacci(n) {
    let fibSeries = [0, 1]; // Starting the series with 0 and 1
    for (let i = 2; i < n; i++) {
        fibSeries.push(fibSeries[i - 1] + fibSeries[i - 2]);
    }
    return fibSeries;
}

// Example: Generate first 10 Fibonacci numbers
let n = 10;
console.log(fibonacci(n));

Let’s break down this code step by step:


1. Function Definition (fibonacci(n))

The function fibonacci(n) is designed to generate the Fibonacci series up to the nth term. It accepts one argument n, which determines how many Fibonacci numbers the function will generate.

2. Initialization of the Fibonacci Series (fibSeries)

The Fibonacci sequence starts with two numbers: 0 and 1. In the code, we initialize an array fibSeries with the first two Fibonacci numbers:

let fibSeries = [0, 1];
  • fibSeries[0] = 0 (first number)
  • fibSeries[1] = 1 (second number)

These are the starting points of the Fibonacci series.

3. Generating Fibonacci Numbers with a Loop

Now, the main part of the code is a for loop:

for (let i = 2; i < n; i++) {
    fibSeries.push(fibSeries[i - 1] + fibSeries[i - 2]);
}

Let’s break this down:

  • The loop starts with i = 2 because the first two numbers (0 and 1) are already present in the fibSeries array.
  • The loop runs until i < n, ensuring that the series stops at the nth term.
  • Inside the loop, the new Fibonacci number is calculated by adding the two preceding numbers: fibSeries[i - 1] and fibSeries[i - 2].
  • This new number is then added to the fibSeries array using push().

For example:

  • When i = 2, the next number is fibSeries[1] + fibSeries[0] = 1 + 0 = 1.
  • When i = 3, the next number is fibSeries[2] + fibSeries[1] = 1 + 1 = 2.
  • This process continues for the desired number of terms.

4. Returning the Fibonacci Series

Once the loop completes, the function returns the fibSeries array, which now contains the Fibonacci sequence up to the nth term.

return fibSeries;

5. Example: Generating the First 10 Fibonacci Numbers

Let’s see how this works in practice. We call the fibonacci() function with n = 10:

let n = 10;
console.log(fibonacci(n));

This will print the first 10 numbers of the Fibonacci series:

[ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ]

Conclusion

The Fibonacci series is a simple yet powerful concept in mathematics and computer science. By using a straightforward loop and array manipulation, we can generate the series up to any term specified by the user.

This JavaScript implementation is efficient for small to medium values of n. However, for very large values of n, the performance can be improved by using techniques like memoization to avoid redundant calculations.

This code serves as a great starting point for understanding not only Fibonacci numbers but also how loops and arrays work in JavaScript. It’s a great exercise for beginners looking to enhance their understanding of programming logic and algorithms.

Leave A Comment