Find the Fibonacci Series up to Nth Term in Javascript Language

Find the Fibonacci Series up to Nth Term in Javascript Language

We will discuss three methods to solve this problem using Javascript

Hi everyone today we Find the Fibonacci Series up to Nth Term in Javascript Language. let's discuss how to solve this problem statement. if you want to check out yesterday's problem click here.

Given an integer input for the Nth value, the objective is to Find the Fibonacci Series up to the Nth term using Loops and recursion in Javascript Language. To do so we’ll use the following methods,

  • Method 1: Using Iteration

  • Method 2: Using Recursion

  • Method 3: Using Formula

Method 1: Using Iteration

Algorithm

  • step 1: Initialize the value of num to 15.

  • step 2: Initialize variables a and b to 0 and 1 respectively.

  • step 3: Print the values of a and b separated by a comma and a space using console.log().

  • step 4: Initialize a variable nextTerm.

  • step 5: For each value of i from 2 to num (exclusive):

    1. Calculate the value of nextTerm as the sum of a and b.

    2. Assign the value of b to a.

    3. Assign the value of nextTerm to b.

    4. Print the value of nextTerm followed by a comma and a space using console.log().

  • step 6: End the loop.

Javascript Code

let num = 15;
let a = 0, b = 1;

// Here we are printing 0th and 1st terms
console.log(a + " , " + b + " , ");

let nextTerm;

// printing the rest of the terms here
for (let i = 2; i < num; i++) {
  nextTerm = a + b;
  a = b;
  b = nextTerm;
  console.log(nextTerm + " , ");
}

Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Method 2: Using Recursion

Algorithm

  • step 1: Initialize variables a and b to 0 and 1 respectively.

  • step 2: Initialize a variable nextTerm.

  • step 3: Define a function fib that takes an integer n as input.

  • step 4: If n is greater than 0, do the following:

    1. Calculate the value of nextTerm as the sum of a and b.

    2. Assign the value of b to a.

    3. Assign the value of nextTerm to b.

    4. Print the value of nextTerm followed by a comma and a space using console.log().

    5. Recursively call the fib function with n - 1 as the input.

  • step 5: End the if statement.

  • step 6: Return 0.

  • step 7: Initialize the value of n to 15.

  • step 8: Print the values of a and b separated by a comma and a space using console.log().

  • step 9: Call the fib function with n - 2 as the input.

Javascript Code

let a = 0, b = 1, nextTerm;

function fib(n) {
  if (n > 0) {
    nextTerm = a + b;
    a = b;
    b = nextTerm;

    console.log(nextTerm + " , ");
    fib(n - 1);
  }
  return 0;
}

let n = 15;
// Here we are printing 0th and 1st terms
console.log(a + " , " + b + " , ");

// n-2 as 2 terms already printed
fib(n - 2);

Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Method 3: Using Formula

Algorithm

  • step 1: Initialize variables a to 0 and b to 1.

  • step 2: Define a function fib that takes a parameter n.

  • step 3: Inside the fib function, create an array f of length n+1.

  • step 4: Set the first two elements of the array f[0] to 0 and f[1] to 1.

  • step 5: Print the values of the first two elements of the array.

  • step 6: Using a loop, compute and store the fibonacci sequence in the array f for values of i from 2 to n-1.

  • step 7: For each value of i, compute the sum of the previous two elements of the array f and store the result in f[i].

  • step 8: Print each value of the array f computed in the previous step.

Javascript Code

let a = 0, b = 1, nextTerm;

function fib(n) {
  let f = new Array(n + 1);

  f[0] = 0;
  f[1] = 1;

  console.log(f[0] + " , " + f[1] + " , ");

  for (let i = 2; i < n; i++) {
    f[i] = f[i - 1] + f[i - 2];
    console.log(f[i] + " , ");
  }
}

let n = 15;
fib(n);

Output

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377

Conclusion

So this article we solved the problem statement using javascript language. I hope you found this article helpful for your web development journey.

  • If you fill this article informative please like this article, if you have any suggestions please comment on this article, share it on social media and follow me on

  • Hashnode - Gaurav Patil

  • Twitter - @GauravYPatil