Skip to main content
✂️ Text & String Tools

What you're building

A lineDiff(original, revised) function that compares two multi-line strings and returns a structured list of changes — the core of every version-control diff tool.

Requirements

  • Split both strings on newline ("\n").
  • Return an array of change objects, one per line that differs.
  • Each change object must have the shape: { line: number, type: "added" | "removed" | "changed", original?: string, revised?: string } where line is the 1-based line index in the longer of the two strings.
  • A line is removed if it exists in original but has no counterpart in revised (revised is shorter at that index).
  • A line is added if it exists in revised but has no counterpart in original (original is shorter at that index).
  • A line is changed if both versions have a line at that index but the content differs.
  • Lines that are identical produce no entry.
  • This is a positional diff (line N vs line N), NOT a Myers diff — no LCS needed.

Example:

original: "hello\nworld"
revised:  "hello\nearth\nextra"
→ [
    { line: 2, type: "changed", original: "world", revised: "earth" },
    { line: 3, type: "added", revised: "extra" }
  ]

Stretch goals

  • Add a context option that includes N unchanged lines around each change.
  • Render the output as a unified diff string (with +/- prefixes).

What this proves

You can iterate two collections in lockstep, handle length mismatches gracefully, and emit structured diagnostic output — skills that appear in code review tools, deployment pipelines, and config management systems.

⚡ 60 XP
solution.jsRuns in your browser
Loading editor…
Tests4 to pass
identical strings return no changes
detects changed line
detects added lines
detects removed lines

Run your code to check these.