In this quick coding exercise I implement the Fibonacci sequence using Javascript, allowing the user to prompt how many numbers in the sequence they would like to calculate. The function will then return that many numbers in the sequence.
The Fibonacci Sequence is a sequence of numbers beginning with a set of two integers: 0 and 1 or 1 and 1. The next number in the sequence is the sum of the previous two. For instance:
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...etc.
Implementing the function itself was simple enough, but I learned an even more valuable lesson about Javascript closures along the way.
The Problem
The algorithm itself wasn’t exactly tricky. In fact, the Fibonacci sequence itself was only a few lines of code:
Indeed, the hurdle I had with this project came when I tried to implement the user interface for the application. I wanted to display the resulting sequence in a list, with each new number displaying a moment after the previous with a “fade-in” transition.
This was simple enough: write a for loop and append a child for each number in the sequence to the list:
However, for some reason when I attempted to access the classList of the <li>
element, the only element to receive the update was the last. Curiously, I had assumed that the for
loop and setTimeout
function would be enough to create closure, but that was not the case. After scratching my head a bit, I asked for help, which of course led me to StackOverflow. (Thank you, jfriend000, for the solution!)
In order to capture each individual <li>
element, I would need to encapsulate the entire bit of code within a closure in the form of an immediately invoked function expression, or IIFE for short. By encapsulating the call within a function expression, complete with the unique <li>
element, I was able to use the asynchronous setTimeout
call within and successfully bind it to each number in the sequence:
Once I had solved the problem capturing the functionality within the closure, the rest of the functionality fell right into place. However, there was one thing that was still a bit off… Why was I using a for loop in the first place? Javascript is, after all, a functional language. I could just call .forEach()
on the array and create a closure that way! (Indeed, upon searching I discovered another user with a very similar problem and a much more elegant solution!)
Now that I’ve encapsulated the closure within the function argument, I’ve cleaned up my code and removed the need for an IIFE. In addition, I’ve taken full advantage of the Array object and its functionality.
You can see the final demo here:
Conclusion
While I had known about the functionality of IIFE’s and the Array object before, I hadn’t thought to use either on my first pass-through. Through just a few refactoring iterations, I was able to implement a common functional Javascript pattern and solve a problem along the way!
This is the first post in a long series covering the various coding projects I discover on the web. This specific project comes from a GitHub repository by the user Karan Goel simply titled “Projects”. If you’re looking to tackle some projects of your own, it’s a great resource for dozens of ideas and the project list is designed to be used with any programming language.
Additional reading:
- Getting Closure by Mark Dalgleish
- Demystifying JavaScript Closures, Callbacks and IIFEs
- 5 Array Methods That You Should Be Using Right Now
Images courtesy of jitze and dullhunk via everystockphoto.com, using the CC v2.5 license.