Divide by Zero, Dead in the Water — JavaScript Bug Hunt

Modelled on the USS Yorktown (September 1997): a crew member entered a 0 into a remote database field.

  • Language: JavaScript
  • Layer: Backend
  • Difficulty: Easy
  • Concepts: Error Handling, Validation
  • Modelled on: USS Yorktown · 1997
  • Visible tests: a healthy set of readings averages correctly; a zero divisor is rejected, not propagated
  • Reward: 50 XP for a complete fix

Briefing

Modelled on the USS Yorktown (September 1997): a crew member entered a 0 into a remote database field. The propulsion control software divided by it, the exception cascaded through the network, and the guided-missile cruiser lost propulsion and had to be towed back to port.

calibration.js computes a ratio from operator-supplied readings and has the same missing guard.

Fix computeRatio so a zero divisor is rejected rather than propagated.

Bug report

BUG-DDG48 · Priority: Critical (loss of propulsion) · Reported by: engineering watch

computeRatio(readings):

  • returns { ok: true, value } when every divisor is non-zero
  • returns { ok: false, value: 0 } when any divisor is zero — the caller then falls back to the last good value instead of shutting down

Observed: a zero divisor produces Infinity (or NaN), which then poisons every downstream average and takes the whole control loop with it.

Logs

[prop] ratio=Infinity from divisor=0 at index 2
[prop] control loop aborted; manual override required

The code as shipped

src/control/calibration.js (editable)

// Averages numerator/divisor across all readings.
exports.computeRatio = function (readings) {
  var total = 0;
  for (var i = 0; i < readings.length; i++) {
    total += readings[i].numerator / readings[i].divisor;
  }
  return { ok: true, value: total / readings.length };
};

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