Find the Fibonacci Series up to Nth Term in Javascript Language
We will discuss three methods to solve this problem using Javascript

I am a Full stack developer.
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
numto 15.step 2: Initialize variables
aandbto 0 and 1 respectively.step 3: Print the values of
aandbseparated by a comma and a space usingconsole.log().step 4: Initialize a variable
nextTerm.step 5: For each value of
ifrom 2 tonum(exclusive):Calculate the value of
nextTermas the sum ofaandb.Assign the value of
btoa.Assign the value of
nextTermtob.Print the value of
nextTermfollowed by a comma and a space usingconsole.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
aandbto 0 and 1 respectively.step 2: Initialize a variable
nextTerm.step 3: Define a function
fibthat takes an integernas input.step 4: If
nis greater than 0, do the following:Calculate the value of
nextTermas the sum ofaandb.Assign the value of
btoa.Assign the value of
nextTermtob.Print the value of
nextTermfollowed by a comma and a space usingconsole.log().Recursively call the
fibfunction withn - 1as the input.
step 5: End the if statement.
step 6: Return 0.
step 7: Initialize the value of
nto 15.step 8: Print the values of
aandbseparated by a comma and a space usingconsole.log().step 9: Call the
fibfunction withn - 2as 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
ato 0 andbto 1.step 2: Define a function
fibthat takes a parametern.step 3: Inside the
fibfunction, create an arrayfof lengthn+1.step 4: Set the first two elements of the array
f[0]to 0 andf[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
ffor values ofifrom 2 ton-1.step 7: For each value of
i, compute the sum of the previous two elements of the arrayfand store the result inf[i].step 8: Print each value of the array
fcomputed 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




