avatarMonu Kumar Modi

Summary

The article provides two methods for finding the nth Fibonacci number in JavaScript: an iterative approach using a for loop and a recursive approach.

Abstract

The article "Coding: Find the nth Fibonacci Number in JavaScript" delves into the Fibonacci sequence, explaining that each number is the sum of the two preceding ones, starting with 0 and 1. It presents two JavaScript functions to calculate the nth Fibonacci number. The first method uses an iterative approach with a for loop, which is efficient and avoids the overhead of multiple function calls. The second method employs recursion, which is straightforward but can be inefficient for large values of n due to its exponential time complexity. The article includes code snippets and explanations for both methods, concluding that the iterative approach is preferable for performance reasons.

Opinions

  • The iterative method is considered more efficient than the recursive method due to its linear time complexity and avoidance of stack overflow issues.
  • The recursive method is acknowledged for its simplicity and ease of understanding but is not recommended for large n values because of its exponential time complexity, which can lead to significant performance degradation.
  • The article implies that developers should be aware of the trade-offs between simplicity and performance when choosing between iterative and recursive approaches for calculating the Fibonacci sequence.

Coding: Find the nth Fibonacci Number in JavaScript

In this article, we will be discussing a code snippet in which we will find the nth Fibonacci number.

Fibonacci Series: The Fibonacci sequence is a series of numbers in which each number is the sum of the two preceding numbers. The sequence starts with 0 and 1, and the subsequent numbers are calculated as the sum of the previous two.

Method 1: Using For loop

nth Fibonacci number

Let’s go through the code line by line to understand how it works:

function fibonacci(n) {

This line defines a function named fibonacci which takes an integer parameter n. The function will return the nth number in the Fibonacci sequence.

let a = 0;
let b = 1;

Here, we define two variables a and b, which represent the first two numbers in the sequence.

if (n === 0) {
  return a;
}

If the input parameter n is equal to zero, we simply return the first number in the sequence, which is 0.

for (let i = 2; i <= n; i++) {
  let c = a + b;
  a = b;
  b = c;
}

This loop calculates the nth number in the sequence by iterating from the third number (i.e., i=2) up to the nth number. In each iteration, we calculate the sum of the previous two numbers (a and b) and store the result in a new variable c. We then update the value of a and b to the values of b and c, respectively.

return b;

After the loop is completed, we return the value of b, which represents the nth number in the sequence.

console.log(fibonacci(3));

Finally, we call the fibonacci function with an input parameter of 3, which should return the 3rd number in the Fibonacci sequence. The result of the function call is then printed to the console using the console.log function.

Complete Code:

function fibonacci(n) {
  let a = 0;
  let b = 1;

  if (n === 0) {
    return a;
  }

  for (let i = 2; i <= n; i++) {
    let c = a + b;
    a = b;
    b = c;
  }

  return b;
}

console.log(fibonacci(3));

Overall, the function uses a simple iterative approach to calculate the nth number in the Fibonacci sequence. This implementation is more efficient than a recursive approach, as it avoids the overhead of function calls and stack frames.

Method 1: Using Recursion

Let’s go through the code line by line to understand how it works:

function fibonacci(n) {

This line defines a function named fibonacci which takes an integer parameter n. The function will return the nth number in the Fibonacci sequence.

if (n <= 1) {
  return n;
}

This is the base case of the recursive function. If the input parameter n is less than or equal to 1, we simply return the value of n. This is because the first two numbers in the Fibonacci sequence are 0 and 1, and any value less than or equal to 1 will return the same value.

return fibonacci(n - 1) + fibonacci(n - 2);

This is the recursive case of the function. If the input parameter n is greater than 1, we recursively call the fibonacci function with two input parameters: n-1 and n-2. We then return the sum of these two function calls, which is the nth number in the Fibonacci sequence.

console.log(fibonacci(2));

Finally, we call the fibonacci function with an input parameter of 2, which should return the 2nd number in the Fibonacci sequence. The result of the function call is then printed to the console using the console.log function.

Complete Code:

function fibonacci(n) {
  if (n <= 1) {
    return n;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
}

console.log(fibonacci(2));

Overall, the function uses a simple recursive approach to calculate the nth number in the Fibonacci sequence. However, this implementation has a time complexity of O(2^n) and can become very slow for large values of n. This is because the function recursively calls itself twice for each value, leading to an exponential increase in the number of function calls. Therefore, it is not recommended to use this implementation for large values of n.

JavaScript
Interview
Coding
Fibonacci
Recommended from ReadMedium