Here's a debugging technique that sounds like a joke, but genuinely works. It was popularized by a famous programming book called The Pragmatic Programmer.
- Get a rubber duck. (Or a teddy bear. Or a houseplant. Or a very patient friend.)
- Put it next to your computer.
- Explain your code to the duck, line by line, out loud. What does each line do? What should the value of each variable be at this point?
- Somewhere in the middle of your explanation, you'll say something like "and then this adds the price to the total, starting from index one, because... oh. Oh no."
Why does this work? Because when you explain something out loud, you're forced to slow down and think about every single step, instead of skimming over the parts you assume are correct. And bugs love to hide in the parts you assume are correct.
The duck doesn't need to say anything. The duck is very wise.