Practice / macOS

Practice test-driven development

Take Two Sum through Red, Green, and Refactor in the JavaScript TDD workspace.

TDD Practice with the Red, Green, and Refactor workflow and test editor.
TDD Practice with the Red, Green, and Refactor workflow and test editor. Select the image to enlarge.
  1. Write one test in Red

    Open Red. In Learner Tests, fill in the blank test or use Add → Add Blank Test. Enter a Test name, Arguments JSON, and Expected JSON. Choose Establish Red to run against the read-only starter.

    Example

    Name the test Uses two distinct indices. Enter [[3, 3], 6] in Arguments JSON and [0, 1] in Expected JSON. The starter returns []. Expect Phase complete with one assertion failure and zero execution errors. If Test Evidence reports a runtime error or timeout, resolve that first.

    Red is complete with one expected assertion failure against the read-only Two Sum starter.
    The new test detects the missing behavior in the starter. Select the image to enlarge.
  2. Implement the smallest fix in Green

    Open Green. Paste the implementation below into the editor and choose Run Green. Test Evidence should show four passing checks with the single learner test from Red.

    Example

    Start with a pair scan. Begin the inner loop at i + 1 so the two positions are always distinct and the smaller index comes first.

    export function solve(nums, target) {
      for (let i = 0; i < nums.length; i++) {
        for (let j = i + 1; j < nums.length; j++) {
          if (nums[i] + nums[j] === target) return [i, j];
        }
      }
      return [];
    }
    Green is complete with four of four checks passing for the nested-loop implementation.
    The learner test and visible contract pass in Green. Select the image to enlarge.
  3. Refactor with evidence

    Open Refactor. Inside the inner loop, extract the sum into a local variable and compare that variable with target. Choose Verify Refactor to run the learner test, the visible contract, and the masked checks.

    Example

    Replace the condition with the two lines below. With one learner test, Test Evidence should show twelve of twelve checks passing. For another practice cycle, try [[3, 2, 4], 6] with expected [1, 2] to catch a solution that checks only the first pair.

    const sum = nums[i] + nums[j];
    if (sum === target) return [i, j];
    Refactor is complete and Test Evidence reports twelve of twelve checks passing.
    Verify Refactor passes the full contract. Select the image to enlarge.

Check your work

Red, Green, and Refactor each show completion. You can explain which assertion established Red and why the Refactor kept the same results.

Try it yourself

After the verified cycle, compare the pair scan with a map implementation in a separate practice attempt. Use a learner test with repeated values and keep the check-before-insert order.

What to look for
The implementation should return [0, 1] for [[3, 3], 6]. In the Refactor phase, use the full verification to catch any change to the original contract.