Hi what do you do when you have coded something to do something specific and it does it but then screws up after? I have gone over my coding logic and still cannot work out why the heck it is not doing what I want it to. What is the best way to try and work out why your code is not doing what you want it to even though the logic appear correct?
Put breakpoints in your code, then examine to make sure you are getting what you are expecting to get at that point in time.
Stepping through the logic can help, the "breakpoints" as mmerlinn suggested being good for this. Watching the values of variables going in and/or as they are processed also helps, which is why under the "developer tools" part of most browsers not only can you use the document inspector, you can watch the console (outputting your variables manually as needed), or add watches and breakpoints under the "debugger" tab. Sometimes though, even with that the answer will elude you because YOU know how it's supposed to work, which can make you blind to something simple like a typo. There's no shame in that, it's a common affliction. It's like the failing editors often have where a simple mistake can elude you. It's the same trick mentalists often use in their stage acts -- "Spell host, spell most, spell ghost, spell roast, what do you put in a toaster?" you say that orally and the majority of people who don't know the joke, will actually respond with "toast"... when the correct answer is bread. Take this simple image as an example: One trick professional writers and editors use to get around this is to read the text backwards. It's not so much that it changes how you think about it, but that it slows you down MAKING you think. It also helps to just walk away for a few hours. Frustration of staring at a problem can actually blind you to it. It's part of why I come to forums like this to help others is sometimes it just helps to go do something else for a while when you hit a brick wall. Solving other people's problems also helps expand your knowledge as different people tend to have different problems from the ones you yourself encounter. But the best answer? Get a second pair of eyes on the problem. Someone who doesn't know your logic won't be making assumptions that you as the original developer will. It's why I encourage people to share their work on forums. Yeah, you might get ripped a new one. Yeah, you might feel foolish if it was something that was staring you in the face. Don't fear that and remember, most people who post are actually trying to help... especially if they give you a different approach and explain the how/what/why.
^^^"Can you find the mistake?" We could if you wouldn't keep distracting us with your stuttering/stammer issue.
Thing is, a LOT of people can't see it until you point it out. Such trickery fascinates me as I'm mostly immune to it, but encounter people who have said problem all the time. It's a really interesting one as it crosses boundaries between the optical illusion and mentalism. It's like if "The Amazing Johnathan" did Gallagher's stage act. There are so many shortcuts the brain takes to get to an answer that lets you get on to the next problem, that actual problems can end up "glossed over" along the way. It's how so many people fail in simple tasks like critical thinking, reacting negatively to same. All because the brain is trying to save a couple watts.
Expanding on this a bit. Almost every day I must verbally get and give long series of alphanumeric digits, like VINs, tracking numbers, part numbers, and tag numbers. To verify that everything was transcribed properly the recipient starts at the end of the number reading everything BACKWARDS, one digit at a time, back to the originator of the information. Further, if the numbers contain letters, when I read them back, I use a DIFFERENT mnemonic than the originator uses, for example, DAVID instead of DELTA for D, or KITCHEN instead of KILO for K, or ZEBRA instead of ZED for Z, or APPLE instead of ANDREW for A. Since it is nigh unto impossible to make the SAME mistake BOTH directions, doing so eliminates any possibility of fatal errors being introduced.