Build a minimal reproduction

Objective

Reduce a failure while preserving the distinction that exposes it. Prerequisites: functions, loops, lists/arrays, and objects/dictionaries in JavaScript.

Learning objective

Reduce a failure while preserving the distinction that exposes it.

Explanation

A long dataset makes it harder to see why an implementation fails. Remove irrelevant elements while keeping the expected and actual results different. Two target occurrences can distinguish first from last; one missing requirement distinguishes all from any.

Write the expected result before changing the code. A minimal reproduction should still exercise the same incorrect assumption as the original failure.

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,target){for(let i=values.length-1;i>=0;i--)if(values[i]===target)return i;return -1;}

const args = [[2, 1, 2], 2];
const before = JSON.stringify(args);
const result = solve(...args);
if (JSON.stringify(result) !== "2") throw new Error("Result regression");
if (JSON.stringify(args) !== before) throw new Error("Input mutation");

Expected result: 2.

A single match hides the bug. Two repeated targets create the smallest meaningful difference between first and last. Regression design: Reduce a long failing list to the shortest list that still distinguishes first from last.

Common mistakes

Do not simplify away the duplicate, ordering, or empty-input feature responsible for the failure.

Guided practice

Reduce a long failing list to the shortest list that still distinguishes first from last. 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

Reduce a failure while preserving the distinction that exposes it. 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.