Fibonacci Program in JavaScript

Introduction

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. This guide will walk you through how to write a JavaScript program to compute the Fibonacci sequence using both iterative and recursive methods.

Problem Statement

Create a JavaScript program that:

  • Accepts an integer n (the position in the Fibonacci sequence).
  • Returns the Fibonacci number at that position.

Example:

  • Input: 5

  • Output: 5 (The sequence is 0, 1, 1, 2, 3, 5)

  • Input: 7

  • Output: 13 (The sequence is 0, 1, 1, 2, 3, 5, 8, 13)

Solution Steps

  1. Read Input: Accept or define the input position n directly in the program.
  2. Handle Base Cases: The first two Fibonacci numbers are 0 and 1.
  3. Compute Fibonacci: Implement two methods—recursive and iterative—to compute the Fibonacci number.
  4. Display the Result: Output the Fibonacci number at position n for each method.

Method 1: Recursive Approach

// JavaScript Program to Find Fibonacci Number Using Recursion
// Author: https://www.javaguides.net/

function fibonacciRecursive(n) {
    if (n === 0) return 0;
    if (n === 1) return 1;
    return fibonacciRecursive(n - 1) + fibonacciRecursive(n - 2);
}

let position = 7; // Example input
let result = fibonacciRecursive(position);
console.log(`The Fibonacci number at position ${position} using recursion is: ${result}`);

Output:

The Fibonacci number at position 7 using recursion is: 13

Explanation:

  • This function uses recursion to calculate the Fibonacci number. The base cases are 0 and 1 for the first two Fibonacci numbers, and for larger values, it calls the function recursively to sum the previous two numbers.

Method 2: Iterative Approach

// JavaScript Program to Find Fibonacci Number Using Iteration
// Author: https://www.javaguides.net/

function fibonacciIterative(n) {
    let a = 0, b = 1, next;
    if (n === 0) return a;
    for (let i = 2; i <= n; i++) {
        next = a + b;
        a = b;
        b = next;
    }
    return b;
}

result = fibonacciIterative(position);
console.log(`The Fibonacci number at position ${position} using iteration is: ${result}`);

Output:

The Fibonacci number at position 7 using iteration is: 13

Explanation:

  • The iterative approach uses a for loop to calculate the Fibonacci number. Starting from 0 and 1, the loop sums the two previous numbers and updates them until it reaches the desired position.

Method 3: Using Memoization (Optimized Recursion)

// JavaScript Program to Find Fibonacci Number Using Memoization
// Author: https://www.javaguides.net/

function fibonacciMemoization() {
    const memo = {};
    
    function fib(n) {
        if (n === 0) return 0;
        if (n === 1) return 1;
        if (memo[n]) return memo[n];
        memo[n] = fib(n - 1) + fib(n - 2);
        return memo[n];
    }

    return fib;
}

const fibonacci = fibonacciMemoization();
result = fibonacci(position);
console.log(`The Fibonacci number at position ${position} using memoization is: ${result}`);

Output:

The Fibonacci number at position 7 using memoization is: 13

Explanation:

  • This optimized recursive approach uses memoization to store previously calculated Fibonacci numbers in a cache (memo), reducing the time complexity from exponential to linear.

Conclusion

This JavaScript program demonstrates three methods for calculating Fibonacci numbers:

  1. Recursion is straightforward but inefficient for large numbers due to redundant calculations.
  2. Iteration is more efficient and uses less memory than recursion.
  3. Memoization combines recursion with caching, significantly improving performance for larger numbers.

Each method has its advantages, depending on the context in which you need to compute Fibonacci numbers.

Comments