Avatar of brunnock

brunnock's solution

to Clock in the JavaScript Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Implement a clock that handles times without dates.

You should be able to add and subtract minutes to it.

Two clocks that represent the same time should be equal to each other.

Setup

Go through the setup instructions for JavaScript to install the necessary dependencies:

http://exercism.io/languages/javascript/installation

Running the test suite

The provided test suite uses Jasmine. You can install it by opening a terminal window and running the following command:

npm install -g jasmine

Run the test suite from the exercise directory with:

jasmine clock.spec.js

In many test suites all but the first test have been marked "pending". Once you get a test passing, activate the next one by changing xit to it.

Source

Pairing session with Erin Drummond https://twitter.com/ebdrummond

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

clock.spec.js

var at = require('./clock').at;

describe('Clock', function () {
  describe('Creating a new clock with an initial time', function () {
    it('on the hour', function () {
      expect(at(8).toString()).toEqual('08:00');
    });

    xit('past the hour', function () {
      expect(at(11, 9).toString()).toEqual('11:09');
    });

    xit('midnight is zero hours', function () {
      expect(at(24, 0).toString()).toEqual('00:00');
    });

    xit('hour rolls over', function () {
      expect(at(25, 0).toString()).toEqual('01:00');
    });

    xit('hour rolls over continuously', function () {
      expect(at(100, 0).toString()).toEqual('04:00');
    });

    xit('sixty minutes is next hour', function () {
      expect(at(1, 60).toString()).toEqual('02:00');
    });

    xit('minutes roll over', function () {
      expect(at(0, 160).toString()).toEqual('02:40');
    });

    xit('minutes roll over continuously', function () {
      expect(at(0, 1723).toString()).toEqual('04:43');
    });

    xit('hour and minutes roll over', function () {
      expect(at(25, 160).toString()).toEqual('03:40');
    });

    xit('hour and minutes roll over continuously', function () {
      expect(at(201, 3001).toString()).toEqual('11:01');
    });

    xit('hour and minutes roll over to exactly midnight', function () {
      expect(at(72, 8640).toString()).toEqual('00:00');
    });

    xit('negative hour', function () {
      expect(at(-1, 15).toString()).toEqual('23:15');
    });

    xit('negative hour rolls over', function () {
      expect(at(-25, 0).toString()).toEqual('23:00');
    });

    xit('negative hour rolls over continuously', function () {
      expect(at(-91, 0).toString()).toEqual('05:00');
    });

    xit('negative minutes', function () {
      expect(at(1, -40).toString()).toEqual('00:20');
    });

    xit('negative minutes rolls over', function () {
      expect(at(1, -160).toString()).toEqual('22:20');
    });

    xit('negative minutes rolls over continuously', function () {
      expect(at(1, -4820).toString()).toEqual('16:40');
    });

    xit('negative hour and minutes both roll over', function () {
      expect(at(-25, -160).toString()).toEqual('20:20');
    });

    xit('negative hour and minutes both roll over continuously', function () {
      expect(at(-121, -5810).toString()).toEqual('22:10');
    });

    describe('Adding and subtracting minutes', function () {
      xit('add minutes', function () {
        expect(at(10, 0).plus(3).toString()).toEqual('10:03');
      });

      xit('add no minutes', function () {
        expect(at(6, 41).plus(0).toString()).toEqual('06:41');
      });

      xit('add to next hour', function () {
        expect(at(0, 45).plus(40).toString()).toEqual('01:25');
      });

      xit('add more than one hour', function () {
        expect(at(10, 0).plus(61).toString()).toEqual('11:01');
      });

      xit('add more than two hours with carry', function () {
        expect(at(0, 45).plus(160).toString()).toEqual('03:25');
      });

      xit('add across midnight', function () {
        expect(at(23, 59).plus(2).toString()).toEqual('00:01');
      });

      xit('add more than one day (1500 min = 25 hrs)', function () {
        expect(at(5, 32).plus(1500).toString()).toEqual('06:32');
      });

      xit('add more than two days', function () {
        expect(at(1, 1).plus(3500).toString()).toEqual('11:21');
      });

      xit('subtract minutes', function () {
        expect(at(10, 3).minus(3).toString()).toEqual('10:00');
      });

      xit('subtract to previous hour', function () {
        expect(at(10, 3).minus(30).toString()).toEqual('09:33');
      });

      xit('subtract more than an hour', function () {
        expect(at(10, 3).minus(70).toString()).toEqual('08:53');
      });

      xit('subtract across midnight', function () {
        expect(at(0, 3).minus(4).toString()).toEqual('23:59');
      });

      xit('subtract more than two hours', function () {
        expect(at(0, 0).minus(160).toString()).toEqual('21:20');
      });

      xit('subtract more than two hours with borrow', function () {
        expect(at(6, 15).minus(160).toString()).toEqual('03:35');
      });

      xit('subtract more than one day (1500 min = 25 hrs)', function () {
        expect(at(5, 32).minus(1500).toString()).toEqual('04:32');
      });

      xit('subtract more than two days', function () {
        expect(at(2, 20).minus(3000).toString()).toEqual('00:20');
      });
    });

    describe('Construct two separate clocks, set times, test if they are equal', function () {
      it('clocks with same time', function () {
        expect(at(15, 37).equals(at(15, 37))).toBeTruthy();
      });

      xit('clocks a minute apart', function () {
        expect(at(15, 36).equals(at(15, 37))).toBeFalsy();
      });

      xit('clocks an hour apart', function () {
        expect(at(14, 37).equals(at(15, 37))).toBeFalsy();
      });

      xit('clocks with hour overflow', function () {
        expect(at(10, 37).equals(at(34, 37))).toBeTruthy();
      });

      xit('clocks with hour overflow by several days', function () {
        expect(at(3, 11).equals(at(99, 11))).toBeTruthy();
      });

      xit('clocks with negative hour', function () {
        expect(at(22, 40).equals(at(-2, 40))).toBeTruthy();
      });

      xit('clocks with negative hour that wraps', function () {
        expect(at(17, 3).equals(at(-31, 3))).toBeTruthy();
      });

      xit('clocks with negative hour that wraps multiple times', function () {
        expect(at(13, 49).equals(at(-83, 49))).toBeTruthy();
      });

      xit('clocks with minute overflow', function () {
        expect(at(0, 1).equals(at(0, 1441))).toBeTruthy();
      });

      xit('clocks with minute overflow by several days', function () {
        expect(at(2, 2).equals(at(2, 4322))).toBeTruthy();
      });

      xit('clocks with negative minute', function () {
        expect(at(2, 40).equals(at(3, -20))).toBeTruthy();
      });

      xit('clocks with negative minute that wraps', function () {
        expect(at(4, 10).equals(at(5, -1490))).toBeTruthy();
      });

      xit('clocks with negative minute that wraps multiple times', function () {
        expect(at(6, 15).equals(at(6, -4305))).toBeTruthy();
      });

      xit('clocks with negative hours and minutes', function () {
        expect(at(7, 32).equals(at(-12, -268))).toBeTruthy();
      });

      xit('clocks with negative hours and minutes that wrap', function () {
        expect(at(18, 7).equals(at(-54, -11513))).toBeTruthy();
      });
    });
  });
});
var Clock = function (hour,minute) {
  hour |= 0;
  minute |= 0;

  this.minutes = hour*60 + minute;

  this.toString = function() {
    if (this.minutes<0) this.minutes += 1440;
    hour = ((this.minutes/60) % 24) | 0;
    minute = this.minutes % 60;
    return padNum(hour) + ':' + padNum(minute);
  }

  this.plus = function(plusMinute) {
    this.minutes += plusMinute;
    return this;
  }

  this.minus = function(minusMinute) {
    this.minutes -= minusMinute;
    return this;
  }

  this.equals = function(clock2) {
    return this.toString() == clock2.toString();
  }
}

function padNum(num) {
  var snum = num.toString();
  if (num<10) snum = '0' + snum;
  return snum;
}

function at (hour,minute) {  
  return new Clock(hour,minute) 
}

module.exports.at=at;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?