Fibonacci Sequence Algorithm with Javascript

Faris Kanbur
CNK Tech
Published in
4 min readSep 27, 2020

What are Fibonacci Numbers ?

Fibonacci numbers are used to create technical indicators using a mathematical sequence developed by the Italian mathematician, commonly referred to as “Fibonacci,” in the 13th century. The sequence of numbers, starting with zero and one, is created by adding the previous two numbers. For example, the early part of the sequence is 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144, 233, 377, and so on.

For more information : https://www.livescience.com/our-solar-system.html

Different methods for Fibonacci Sequence:

You will see that there are many resources and solutions on this subject when you search on the internet.

1.Loops

Examples of this can first be solved with loops.

a. While Loop

While a “while Loop” allows you to run a code, it will terminate your code execution when a specified condition is true but the condition is no longer true.

While doing this with the while loop, we can use different methods in our code by making small changes and get the same result.

Image-1 & Image-2

As you can clearly see here, we first define the function, then we define “while loop” in it and return our result with “return”.As can be seen above, the only change here is that in the first application, we defined the “result” part in the function and we did not have to define [0,1] (as seen in Image two)in the console log while getting the output.However, if you want to reach the last element of the last list, not the number array, you should revise the “return” part as follows.

b. For Loop

The for loop is executed a certain number of times, and if the loop continues to satisfy the specified condition each time it returns, it continues to run in the loop.

In this example, we can reach the results we want by making a small change in the “return” part.We can bring a solution to this situation by adding a small change to the point of view.

I would like to show you the example below with a little change in two ways.

sample - 1 & sample-2

The point to be considered here is that in both examples, while arr = [0,1], in sample one , the value of “i” from zero but in sampe two “i” starting from 2.In order that the lengths of the list we get as a result are equal, the conditions inside the for loop must be adapted accordingly.But the other thing that catches your attention here is that the “arr.push” parts also look different, but if we examine them carefully, we see that they give the same result here.I tried to express this as visually below

Finally, if you want to take the desired number and output it to the screen and you want to do it according to certain conditions, you can do it as follows.

First of all, you must create the input and button part that will allow the user to enter and send the relevant arrangement on the html page.Then you should get the information the user has entered with getElementById.and you need to determine the action(the functions which we mentioned above ) with addEventListener that to be done after pressing the user the button to send input information.if you want to add various conditions or add restrictions regarding the data to be entered, you can specify that it is shown in the above example in html and javaScript files.

--

--

Faris Kanbur
CNK Tech

I am a Full stack developer. I love learning new things and sharing what I learned with everyone because life is more beautiful when people have shared