By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Computing Fibonacci Sequence in JavaScript
JavaScriptJavaScript Program

Computing Fibonacci Sequence in JavaScript

Admin
Last updated: 2023/01/12 at 6:36 AM
Admin
Share
4 Min Read
Computing Fibonacci Sequence in JavaScript

In this article, we are going to learn and see to computing Fibonacci sequence in JavaScript. It is a basic problem, or you can say a program in JavaScript, computing Fibonacci sequence becomes more problematic when you don’t know its main logic. So we are just going to make a Fibonacci sequence with some easier and good way to help out beginners.

Contents
What is Fibonacci Sequence?Program Creation For Fibonacci SequenceFibonacci Sequence Using LoopFibonacci Sequence Using RecursionYou may also like:

What is Fibonacci Sequence?

Fibonacci’s sequence is pretty easy to understand, here we have two input values, one is an initial value and the other one is a number of passes. For example, if we need to find Fibonacci sequence starts from 1 till 5 passes then sequence will be 1,1,2,3,5.

So here we have simple logic in order to create Fibonacci sequence is, we have two initial values, as we have to assign 0,1. Then the 3rd value of this sequence will be 0+1 =1, 4th value will be 1+1 =2, and 5th value will be 1+2 = 3 etc. So basically we are deciding the next number by adding preceding values or two before values.

Program Creation For Fibonacci Sequence

Okay, There are multiple ways to create this sequence, but we will see two basic and easy methods to create this sequence. (1) using loop, (2) using function recursion.

Fibonacci Sequence Using Loop

The first method is to use loop, here we are using while loop, but you can go with for loop as well. Here we have added a variable in which we have added a function with two parameter result and len. result is an array where we will add the result of each loop.

Then we have added two variable num1 and num2 where we have assigned 1st and 2nd value of the array. After that, we have also added a temporary variable “next” and cnt =2, and we are just adding the num1 and num2 and assigning in variable next. Then we assigned value of num2 in num1 and value of next in num2, and we’re just pushing the value of next in to the result array.

 var fibo = function(result, len){
            var num1 = result[0],
                num2 = result[1],
                next,
                cnt = 2;
            
            while(cnt < len){
                next = num1 + num2;
                num1 = num2;
                num2 = next;
                result.push(next);
                cnt++;
            }

            return result;
        }
Computing Fibonacci Sequence in JavaScript

Fibonacci Sequence Using Recursion

Another technique to find Fibonacci sequence is using recursion or function recursion. Function recursion, actually a little bit hard, but it also reduces lines of code. So learning recursion would be good. Basically recursion is a function calls itself, so if the function has some value to do operation then the function dynamically calls itself, so the function will run until we add some break point.

Here we have again added fibo function with result array and length as parameter. Then we will check a condition where the length of the array is greater or equal to the provided length, in that case, we will simply return a result array. Otherwise, we will fetch the second last and last element of the array, and we will perform addition and push its result in the array. Now we will return function call fibo with update array and length to run again function.

var fibo = function fibo(result, len){
            if(result.length>= len){
                return result;
            }

            result.push(result[result.length-2]+result[result.length-1]);

            return fibo(result, len);

        }
Computing Fibonacci Sequence in JavaScript

You may also like:

  • How to Define a Class With Properties and Methods in JavaScript?
  • How to Implement Class Inheritance in JavaScript?
  • How to Find Duplicate Elements in a Given Array?

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

TAGGED: Computing Fibonacci Sequence in JavaScript, Fibonacci Sequence in JavaScript, Fibonacci Series, javascript
Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article how to get duplicate object from array in JavaScript? How To Get Duplicate Object From Array in JavaScript?
Next Article Range and Sum Function Eloquent JavaScript Exercise -Range and Sum Function
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?