The One-Night Overcharge — JavaScript Bug Hunt

Inspired by the fencepost bugs that plague every booking site.

  • Language: JavaScript
  • Layer: Frontend
  • Difficulty: Easy
  • Concepts: Dates, Off-by-one
  • Modelled on: Airbnb-style booking
  • Visible tests: two nights are two nights; same-day stay is zero nights; month boundary is exact
  • Reward: 50 XP for a complete fix

Briefing

Inspired by the fencepost bugs that plague every booking site. Guests booking two nights are being quoted three, and a same-day "day use" booking charges a full night.

The nightly math lives in nights.js — dates arrive as YYYY-MM-DD strings.

Bug report

BUG-1861 · Priority: High · Reported by: pricing

Check-in 2024-06-10, check-out 2024-06-12 is TWO nights. We quote three. Check-in == check-out is ZERO nights. We quote one.

Guests are (rightly) filing refunds.

Logs

[quote] 2024-06-10 -> 2024-06-12 nights=3 (expected 2)

The code as shipped

src/booking/nights.js (editable)

// Nights between an ISO check-in and check-out date (UTC, no times).
var DAY_MS = 24 * 60 * 60 * 1000;

function toUtc(iso) {
  var p = iso.split("-");
  return Date.UTC(parseInt(p[0], 10), parseInt(p[1], 10) - 1, parseInt(p[2], 10));
}

exports.nightsBetween = function (checkin, checkout) {
  var diff = toUtc(checkout) - toUtc(checkin);
  return Math.floor(diff / DAY_MS) + 1;
};

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