Sha256: be98daaafdc4c6c3a2c87bd9bade752082ea9f53178b94993a4ac9931a716442

Contents?: true

Size: 1.08 KB

Versions: 211

Compression:

Stored size: 1.08 KB

Contents

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

  let clock = {
    hour:   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:    clock,
    toString: () => formatNum(clock.hour) + ":" + formatNum(clock.minute),
    plus:     function(minutes) { adjustTime(minutes); return this; },
    minus:    function(minutes) { adjustTime(-minutes); return this; },
    equals:   (otherClock) => clock.hour === otherClock.clock.hour && clock.minute === otherClock.clock.minute
  }
}

Version data entries

211 entries across 211 versions & 1 rubygems

Version Path
trackler-2.2.0.0 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.55 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.54 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.53 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.52 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.51 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.50 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.49 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.48 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.47 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.46 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.45 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.44 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.43 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.42 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.41 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.40 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.39 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.38 tracks/ecmascript/exercises/clock/example.js
trackler-2.1.0.37 tracks/ecmascript/exercises/clock/example.js