Sha256: e4347b5fe0e24c814db73e4cdf812b3c3f11f985a8cfb08dd26351cc9cd037f9

Contents?: true

Size: 1.04 KB

Versions: 185

Compression:

Stored size: 1.04 KB

Contents

export default function (hour, minute) {
  const MINUTESPERDAY = 1440;
  const HOURSPERDAY = 24;

  const clock = {
    hour,
    minute: minute || 0,
  };

  function formatNum(num) {
    const numString = num.toString();
    return numString.length === 1 ? `0${numString}` : numString;
  }

  function adjustTime(delta) {
    delta = Math.abs(delta) >= MINUTESPERDAY ? delta % MINUTESPERDAY : delta;

    const currentMinutes = clock.hour * 60 + clock.minute;
    let newMinutes = (currentMinutes + delta) % MINUTESPERDAY;

    newMinutes = newMinutes < 0 ? newMinutes += MINUTESPERDAY : newMinutes;

    clock.hour = Math.floor(newMinutes / 60) % HOURSPERDAY;
    clock.minute = newMinutes - clock.hour * 60;
  }

  adjustTime(0);

  return {
    clock,
    toString: () => `${formatNum(clock.hour)}:${formatNum(clock.minute)}`,
    plus(minutes) { adjustTime(minutes); return this; },
    minus(minutes) { adjustTime(-minutes); return this; },
    equals: otherClock => clock.hour === otherClock.clock.hour && clock.minute === otherClock.clock.minute,
  };
}

Version data entries

185 entries across 185 versions & 1 rubygems

Version Path
trackler-2.2.0.5 tracks/ecmascript/exercises/clock/example.js
trackler-2.2.0.4 tracks/ecmascript/exercises/clock/example.js
trackler-2.2.0.3 tracks/ecmascript/exercises/clock/example.js
trackler-2.2.0.2 tracks/ecmascript/exercises/clock/example.js
trackler-2.2.0.1 tracks/ecmascript/exercises/clock/example.js