Left-Pad the World — JavaScript Bug Hunt

Inspired by the 11 lines that broke the internet in 2016, when left-pad vanished from npm and half the ecosystem stopped building.

  • Language: JavaScript
  • Layer: Backend
  • Difficulty: Easy
  • Concepts: Strings, Dependencies
  • Modelled on: npm · 2016
  • Visible tests: pads to the target length; long-enough strings pass through; default fill is a space
  • Reward: 50 XP for a complete fix

Briefing

Inspired by the 11 lines that broke the internet in 2016, when left-pad vanished from npm and half the ecosystem stopped building. The team vendored their own replacement — and got those 11 lines wrong.

leftPad.js: pad a string on the left to a target length.

Bug report

BUG-11LOC · Reported by: build infra

leftPad(str, len, ch):

  • pads with ch (default " ") until the string is len long
  • strings already at or beyond len come back untouched

Observed: leftPad("5", 3, "0") returns "0005" (pads len EXTRA chars) and the default fill inserts the literal text "undefined".

Logs

[ids] order number rendered as "undefined42"

The code as shipped

src/util/leftPad.js (editable)

// Pads str on the left with ch until it reaches len characters.
exports.leftPad = function (str, len, ch) {
  str = String(str);
  var pad = "";
  for (var i = 0; i < len; i++) {
    pad += ch;
  }
  return pad + str;
};

Open the hunt to edit the files, run the visible tests and submit against the hidden ones. More JavaScript bug hunts.