7
Warning: my code crashed my browser because of one infinite loop i didn't catch
I was working on a little project last weekend, just a simple calculator in JavaScript for a class I'm taking. I thought everything was fine until I hit run and my whole browser froze for like 30 seconds. I had to force close it and everything. Turns out I wrote a while loop that couldn't ever become false because I forgot to update the variable inside the loop. I spent an hour staring at my code before I found the mistake. It was just one line where I had "i > 0" instead of "i > 0" with an increment step that never ran. I wish someone had told me earlier how important it is to double check your loop conditions before you run them. Has anyone else had a similar moment where a tiny typo cost you a bunch of time?
2 comments
Log in to join the discussion
Log In2 Comments
andrew_sanchez9416d ago
Bro that exact thing happened to me with a while loop a few years ago lol. The line about forgetting to update the variable is so real, I had "x < 10" but never changed x inside the loop and it just ran forever. It's crazy how a tiny mistake like that can bite you so hard. But honestly I think this kind of thing happens way more in everyday life too, not just coding. Like when you're trying to break a bad habit but you keep doing the same thing that keeps the habit going. Or when you're in a relationship and you keep having the same argument but you never actually change what you say or do, so you're stuck in that loop forever. It's like life has its own infinite loops and you gotta find where you forgot to update the variable.
6
carr.daniel15d ago
One time I had this old truck that would only start if I turned the key exactly three times and waited five seconds between each try. Never could figure out why, just accepted it as part of the routine. Then one day it just stopped working completely and I found out the ignition switch was just loose the whole time. Same kinda thing where you keep doing the same weird ritual without ever checking if the actual problem got fixed.
5