$ cat post/debugging-loops.md
Debugging Loops
I’ve been staring at this code for over an hour now. It’s supposed to be simple: just three loops that print out the numbers 1 through 5 in different patterns. But something isn’t right. The output is all jumbled, and I can’t figure out why.
The first loop is supposed to print each number on a new line:
for (let i = 1; i <= 5; i++) {
console.log(i);
}
It works perfectly. Each number on its own line. But the second one is giving me trouble. I wanted to print them side by side, like this:
for (let i = 1; i <= 5; i++) {
console.log(" " + i);
}
The output should be a row of numbers: 1 2 3 4 5. But instead, it looks like the numbers are getting stuck in some kind of loop. Each number is repeating over and over again:
1
1
1
1
1
2
2
2
2
2
...
I’ve tried changing the loop condition, messing with the console.log function, even adding extra spaces before the numbers. Nothing seems to work. It’s frustrating because it’s so basic. I feel like I’m missing something obvious.
The third loop is supposed to print the numbers in reverse order:
for (let i = 5; i >= 1; i--) {
console.log(i);
}
This one is working as expected, printing 5 4 3 2 1 with no issues. But now I’m stuck on this second loop. Maybe it’s time to ask someone for help online or in the coding forum.
Debugging can be tedious, but there’s something oddly satisfying about figuring out why a piece of code isn’t working as intended. It feels like solving a puzzle, and every time you solve one, you learn a little more.