Tutorials Books Videos Forums

-- online Search!

Fibonacci Sequence

by kirupa   |   filed under Coding Exercises

There is a famous sequence of numbers that looks as follows:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

This is the Fibonacci sequence, and the rule behind it is pretty simple. We start with 0 and 1. Every number after that is the sum of the two numbers that came right before it. 0 + 1 gives us 1. 1 + 1 gives us 2. 1 + 2 gives us 3. 2 + 3 gives us 5. You get the idea.

What makes this sequence so popular is that it shows up in nature all the time. Count the spirals on a pinecone, and you'll often find 8 going one way and 13 going the other. Sunflower seeds commonly form 34 and 55 spirals. If we draw squares whose sides match the Fibonacci numbers and connect their corners with quarter circles, we get a spiral that looks a whole lot like a snail's shell.

For this exercise, the goal is to print the Fibonacci sequence to the console. The rules are:

  1. Print the first 20 numbers of the Fibonacci sequence, starting with 0 and 1
  2. Each number must be calculated by your code. No typing the numbers in by hand!
  3. If everything works, the last number you print will be 4181

An example of the output could look as follows:

0
1
1
2
3
5
8
13
...
4181

Looking for some extra credit? Here are two ideas:

Onwards!

Starting Point

Create a new HTML document in your favorite code editor (like Visual Studio Code) and copy/paste the following starting content into it:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fibonacci Sequence</title>
</head>
<body>
  <script>
    
  </script>
</body>
</html>

The HTML you see here is just the bare minimum content needed to help us get our web page up and running. The rest of the content is what you will add entirely on your own! Since our output is printed to the console, be sure to bring up your browser's developer tools to see the results.

Hint: Some Learning Resources

The following tutorials may provide some helpful tips and techniques to help you with this exercise: Console Logging Basics, Looping in JavaScript, Numbers, A Visual Introduction to Arrays, Fibonacci and Going Beyond Recursion

Getting Your Badge

Once you have completed this challenge, you have earned the awesome bragworthy privilege of adding the following badge to your collection:

Fibonacci Sequence badge

To claim it, head over to the forums and respond in the Fibonacci Sequence topic. Be sure to include a link to your solution or insert a copy of your HTML/CSS/JS in the body of the message:

Once you have created your topic, Kirupa will give you a virtual high-five and ensure this badge is added to your list of assigned badges.

Stuck? Need Help? Want a Code Review?

We want to make this a fun learning activity. If you are stuck and need help, please ask on the forums. Please explain your problem in detail and one of the many helpful forum members will help you out.

Similarly, if you want feedback on your code, please also ask in the forums. This would be an excellent chance for you to learn how you can improve your solution from other web developers!

Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.

Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.

Your support keeps this site going! 😇

Kirupa's signature!

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--