Hey everyone! ð
Today, while working on my journey as a computer science student, I was tackling a 2D Arrays challenge in JavaScript. Everything seemed perfect. The logic made sense, the loops looked solid... but the code just wouldn't work.
I spent about 20 minutes staring at the screen, running mental tests, and questioning my programming skills.
Can you spot the error in this snippet?
for(let j = 0; j < matrix.length ; j++){
let value = matrix[j][0];
for(let k = 0; k < matrix[j].lenght; k++){
// Logic here
}
}
Yeah... it was the 't' at the end of matrix[j].lenght. I wrote lenght instead of length.
The JavaScript Trap ðŠĪ
The worst part about this specific typo in JavaScript is that the console won't throw an explicit error. JS just looks at matrix[j].lenght, says "Well, that property doesn't exist, so it's undefined", and moves on.
Since k < undefined evaluates to false, the inner loop never executed, leaving me completely stuck.
My Lesson of the Day ðĄ
I felt a bit silly when I finally noticed that tiny mistake, but it reminded me of two valuable lessons:
Always check the typos first before assuming your entire logic is broken.
Use linters or spell checkers! (Time to install one in my editor ASAP).
If you are also learning to code, don't feel bad when these things happen. It's a classic rite of passage for every developer!
Have you ever lost hours of work because of a single misplaced letter? Let me know in the comments! ð
Follow my journey as I build in public! ð
Top comments (0)