Read errors before changing code
Objective
Translate a traceback or exception into a concrete failing operation. Prerequisites: functions, loops, lists/arrays, and objects/dictionaries in JavaScript.
Translate a traceback or exception into a concrete failing operation.
Explanation
Start at the failing operation and read the call path that led there. An index error, missing property, or zero division describes a symptom; the contract tells you the correct behavior. JavaScript may produce undefined, Infinity, or NaN where Python raises, so the absence of an exception is not proof of correctness.
Reproduce the failure with the smallest relevant input. Check the value and shape immediately before the failing operation. Repair the contract violation instead of catching every exception.
Runnable worked example
The linked exercise begins with flawed code. This repaired example includes a result assertion and an input-preservation assertion.
function solve(values){return values.length?values[0]:null;}
const args = [[]];
const before = JSON.stringify(args);
const result = solve(...args);
if (JSON.stringify(result) !== "null") throw new Error("Result regression");
if (JSON.stringify(args) !== before) throw new Error("Input mutation");
Expected result: null.
Check length before indexing. Zero is a valid first value, not evidence of an empty list. Regression design: Add a case where the first value is zero and explain why a truthiness fallback fails.
Common mistakes
Do not swallow all errors and return a plausible default. That can hide a second bug and falsely signal success.
Guided practice
Add a case where the first value is zero and explain why a truthiness fallback fails. Run the exercise starter first and record the actual failure. Change one operation, rerun the old failure and nearby cases, then explain the cause in one sentence. An exception is evidence; a passing happy path alone is not a diagnosis.
Independent repair exercises
Recap
Translate a traceback or exception into a concrete failing operation. Keep a regression that fails on the old code and passes after the repair. Python and JavaScript tracks teach corresponding debugging concepts with distinct language behavior and independent progress.