Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

1.9 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69c5f3d787b1725d5f00c8ba Challenge 256: Closest Time Direction 28 challenge-256

--description--

Given two times, determine whether you can get from the first to the second faster by moving forward or backward.

  • Times are given in 24-hour format ("HH:MM")
  • The clock wraps around (23:59 goes to 00:00 when moving forward, and 00:00 goes to 23:59 when moving backwards)

Return:

  • "forward" if moving forward is shorter
  • "backward" if moving backward is shorter
  • "equal" if both directions take the same amount of time

--hints--

getDirection("10:00", "12:00") should return "forward".

assert.equal(getDirection("10:00", "12:00"), "forward");

getDirection("11:00", "05:00") should return "backward".

assert.equal(getDirection("11:00", "05:00"), "backward");

getDirection("00:00", "12:00") should return "equal".

assert.equal(getDirection("00:00", "12:00"), "equal");

getDirection("15:45", "01:10") should return "forward".

assert.equal(getDirection("15:45", "01:10"), "forward");

getDirection("03:30", "19:50") should return "backward".

assert.equal(getDirection("03:30", "19:50"), "backward");

getDirection("06:30", "18:30") should return "equal".

assert.equal(getDirection("06:30", "18:30"), "equal");

--seed--

--seed-contents--

function getDirection(time1, time2) {

  return time1;
}

--solutions--

function getDirection(time1, time2) {
  function toMinutes(t) {
    const [h, m] = t.split(":").map(Number);
    return h * 60 + m;
  }

  const start = toMinutes(time1);
  const end = toMinutes(time2);

  const forward = (end - start + 1440) % 1440;
  const backward = (start - end + 1440) % 1440;

  if (forward < backward) return "forward";
  if (backward < forward) return "backward";
  return "equal";
}