thecodezone.
The Code Zone
← Back to the Developer Playbook

Code Zone Bug Lab / Episode 13

The Total That Keeps Growing

The same items cost 10, then 20, then 30. Rob only clicked Calculate again.

DEV-156 · Loops · accumulators · initialization15–20 minutesNo setup or account required
An unchanged cart total grows from ten dollars to twenty after recalculating.
An unchanged cart total grows from ten dollars to twenty after recalculating.

Your sprint ticket · In progress · DEV-156

The Total That Keeps Growing.

Customer report: The prices are unchanged, but every calculation adds them to the previous total.

Steps to reproduce

  1. Keep prices 2, 3, and 5.
  2. Click Calculate total twice.
  3. Switch to the alternate prices and calculate again.

Make a focused behavior fix. Styling and adding unrelated features belong to another ticket.

Acceptance criteria

  • The starter prices total 10.
  • Calculating the same prices again still totals 10.
  • Alternate prices 4 and 6 total 10 after any previous calculation.
  • An empty price list totals 0.
  • Decimal prices 1.5 and 2.25 total 3.75.

Learning demo only. No real accounts, purchases, authentication, or external requests.

Reproduce → inspect → hypothesize → verify

Take the ticket.

Reproduce the bug before editing. Inspect the Console and the JS tab. Explain one hypothesis, change the relevant code, then use Check challenge below the editor. Test the full ticket, not only the starter example. Reveal hints one at a time if needed.

Checking account and saved lab work…

Sign in to sync across devices

Loading the JavaScript and HTML editor…

Pass the checks or make three attempts to unlock an explained example.

Ready to test? Choose “Check challenge” after investigating.

    Open starter in a separate tab

    Code and prediction notes recover in this browser and sync when signed in. Instructor examples stay separate. Reset replaces code, not prediction notes.

    One possible solution

    Compare this approach with your own. Opening it does not change your code or mark the exercise complete.

    Declare let total = 0; inside sumPrices. Keeping it outside carries previous calculations into the next one. Return the new sum after the loop. Test repeated calls, changed prices, and an empty array.

    Try first. Reveal one clue at a time.

    Need a lead?

    1. Watch the accumulator.

    What value does total have when the second calculation starts?

    2. Find the initialization.

    Does the accumulator belong to one calculation, or is it shared between calculations?

    3. Start each sum at zero.

    Move let total = 0 inside sumPrices so each call starts fresh.

    Your lab report

    Show your reasoning.

    “I expected… I observed… The console showed… My hypothesis was… I changed… I verified…”

    Use Check challenge for automated behavior checks. Passing checks are useful evidence, not a complete code review.

    Give others room to investigate: label solutions as spoilers.

    Share your lab report on SKOOL (new tab) ↗