Spot the Bug #1 - The Mysterious JavaScript Loop

📆 · ⏳ 2 min read ·

Welcome to the very first edition of “Spot the Bug”

What’s “Spot the Bug”?

In this category, we’ll learn about the art of debugging, where even the tiniest mistakes can lead to big headaches.

Each edition, I’ll present a small piece of code, and your mission, should you choose to accept it, is to identify the bug lurking within.

It’s a fun and interactive way to sharpen your coding skills while uncovering the mysteries of software flaws.

Now, without further ado, let’s jump into our inaugural challenge!

Spot the Bug #1 — The Mysterious JavaScript Loop

Consider the following JavaScript code snippet:

function countToFive() {
for (var i = 1; i <= 5; i++) {
setTimeout(function() {
}, 1000);

We wanted to create a function called countToFive which counts from 1 to 5, but for some reason it is actually not working as expected.

Your task is to spot the bug in this code. It might seem deceptively simple at first glance, but there’s a sneaky issue hidden in there. What will be logged to the console when you run this code, and why?

Take a moment to think, and when you’re ready, hit reply with your answer!

Next Week — The Reveal

In our next newsletter of “Spot the Bug”, I’ll not only reveal the bug but also explain why it occurs and how to fix it. It’s an opportunity to learn and grow together, and I can’t wait to share the insights with you.

So, put on your detective hat and take a shot at solving this coding mystery. I can’t wait to see your responses!

Remember, it’s all about the journey of discovery and learning. Stay curious, keep coding, and I’ll be back with the solution soon.

Happy bug hunting!

Akash Rajpurohit


Need help with your software project? Let’s talk