This article is an introduction to the Fibonacci sequence in JavaScript from the basic level. We will start by looking at the Fibonacci sequence generally, and how it works. Lastly, we will look at how we can implement the Fibonacci sequence in Javascript.
The Fibonacci sequence is a technique that displays and increases exponential growth over time. It’s also one of the four approaches to recursion research. It’s an excellent technique to learn how to write recursive code in programming. This is why understanding how Fibonacci sequences work and how to write recursive code is necessary.
Prerequisites
To follow through this course, you need a basic knowledge of JavaScript, such as variables, arrays, functions, for loops, but you don’t need any prior experience implementing a Fibonacci sequence in javascript as this tutorial will give you an in-depth explanation of the Fibonacci sequence and how to implement it in Javascript.
Table of Contents:
- Introduction to Fibonacci Sequence
- How Fibonacci Sequence Works
- Why You Should Learn the Fibonacci Sequence
- Understanding Fibonacci Sequence in Javascript
- Conclusion
Introduction to Fibonacci Sequence
The Fibonacci sequence is a sequence of numbers in which you add the two previous numbers in a sequence to obtain each successive number in the sequence. The sequence starts with 0 and 1 and proceeds forth as 0, 1, 1, 2, 3, 5, 8, 13, and so on.
The Fibonacci sequence is useful in applications of mathematics, science, computers, art, and nature. You can also use it to write recursive code in programming.
The sequence is named after the Italian mathematician Leonardo Bonacci, who lived from 1170 – to 1250.
How Fibonacci Sequence Works
Now we have seen a brief introduction to the Fibonacci sequence, let’s look at the practical example of how it works.
Firstly, we define the Fibonacci sequence as an integer where the first two terms are 0 and 1. Then we define the next term as the sum of the previous two terms.
Below is a code breakdown of how the Fibonacci sequence works.
Example:
The first two terms of the sequence are 0 and 1.
0+1=1
When you add the previous two numbers, you get 2.
The next sequence goes like this:
1+1=2 0,1,1,2
When you add the previous two numbers, you get 3.
1+2=3 0, 1, 1, 2, 3
When you add the previous two numbers, you get 5.
2+3=5 0, 1, 1, 2, 3,5
When you add the previous two numbers, you get 8.
3+5=8 0, 1, 1, 2, 3,5,8
When you add the previous two numbers, you get 13.
5+8=13 0, 1, 1, 2, 3,5,8,13
When you add the previous two numbers, you get 21.
8+13=21 0, 1, 1, 2, 3,5,8,13,21
In the sequence above, if we keep on adding the two previous numbers to the next number it will produce a new number which we will add to the next number to get a new number and so on. That’s how the Fibonacci sequence is being implemented in general.
Now, let’s look at another example:
In this example, we will look at the sum of first 14 fibonacci numbers.
The first 14 fibonacci numbers is given as:
0, 1, 1, 2,3,5,8,13,21,34,55,89,144,233
When we sum up all the values above, the sum of the 14 Fibonacci numbers will be 609.
0+1+1+2+3+5+8+13+21+34+55+89+144+233 = 609
Why You Should Learn the Fibonacci Sequence
There are so many reasons the Fibonacci sequence is ideal in Programming and the rest.
- The Fibonacci sequence can be used to write recursive code in programming.
- It is also implemented in different patterns.
Understanding Fibonacci Sequence in Javascript
We have gone through a basic explanation of how to implement the Fibonacci sequence and why you should learn it.
Now, let’s look at an example on how to implement in Javascript as that is the focus of this article.
Example One: Using for loop to solve a Fibonacci sequence
var a = 0, b= 1; for(var i=0, i<= 10; i++){ console.log(i) }
If we print the above code on our console, we will receive an output like this:
0,1,2,3,4,5,6,7,8,9,10
Now, if you look closely at the output numbers from the console, we have 9 and 10 as the two previous numbers. 9 and 10 are not Fibonacci numbers, because if we sum up the two previous numbers, which are 8 and 9, we won’t get 10 as the value. Remember, we said a Fibonacci number is created from the sum of the last two preceding numbers.
Now, let’s look at another example:
var c = 0, d= 1; console.log(c); console.log(d); for(var i=0, i<= 10; i++){ var fib = c+d console.log(fib) c=d; d=fib; }
Output:
0,1,1,2,3,4,5,6,7,8,13,21,34,55,89,144
The above output is an example of a Fibonacci number, where you add the two previous numbers to get the next number.
Example Two: Finding the 11th number of a Fibonacci sequence
In this new example, we will find the 11th number of a fibonacci sequence
let fibNum = [0, 1] for (var i = 2; i <= 10; i++) { fibNum[i] = fibNum[i - 1] + fibNum[i - 2]; } console.log(fibNum)
Output:
(11) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
The 11th number in the Fibonacci sequence is 55; we simply achieved that by using a for loop in Javascript to iterate through the numbers.
Conclusion
It’s fairly simple reasoning that involves adding the sequence’s previous two terms together to get the next number. In this article, we have covered the basic definition of the Fibonacci sequence and how to implement it in Javascript.
To learn more about what you can do with JavaScript, take a look at this article, “How to Build an FAQ Page With HTML and Javascript.”