Skip to content

Commit d01be58

Browse files
author
programmiri
committed
Add exercise clock
1 parent d32056b commit d01be58

File tree

6 files changed

+4518
-0
lines changed

6 files changed

+4518
-0
lines changed

clock/.eslintrc

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"root": true,
3+
"parser": "babel-eslint",
4+
"parserOptions": {
5+
"ecmaVersion": 7,
6+
"sourceType": "module"
7+
},
8+
"env": {
9+
"es6": true,
10+
"node": true,
11+
"jest": true
12+
},
13+
"extends": [
14+
"eslint:recommended",
15+
"plugin:import/errors",
16+
"plugin:import/warnings"
17+
],
18+
"rules": {
19+
"linebreak-style": "off",
20+
21+
"import/extensions": "off",
22+
"import/no-default-export": "off",
23+
"import/no-unresolved": "off",
24+
"import/prefer-default-export": "off"
25+
}
26+
}

clock/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Clock
2+
3+
Implement a clock that handles times without dates.
4+
5+
You should be able to add and subtract minutes to it.
6+
7+
Two clocks that represent the same time should be equal to each other.
8+
9+
## Setup
10+
11+
Go through the setup instructions for Javascript to
12+
install the necessary dependencies:
13+
14+
[https://exercism.io/tracks/javascript/installation](https://exercism.io/tracks/javascript/installation)
15+
16+
## Requirements
17+
18+
Install assignment dependencies:
19+
20+
```bash
21+
$ npm install
22+
```
23+
24+
## Making the test suite pass
25+
26+
Execute the tests with:
27+
28+
```bash
29+
$ npm test
30+
```
31+
32+
In the test suites all tests but the first have been skipped.
33+
34+
Once you get a test passing, you can enable the next one by
35+
changing `xtest` to `test`.
36+
37+
38+
## Source
39+
40+
Pairing session with Erin Drummond [https://twitter.com/ebdrummond](https://twitter.com/ebdrummond)
41+
42+
## Submitting Incomplete Solutions
43+
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

clock/babel.config.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
useBuiltIns: false,
10+
},
11+
12+
],
13+
],
14+
};

clock/clock.spec.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
import { at } from './clock';
2+
3+
describe('Clock', () => {
4+
describe('Creating a new clock with an initial time', () => {
5+
test('on the hour', () => {
6+
expect(at(8).toString()).toEqual('08:00');
7+
});
8+
9+
xtest('past the hour', () => {
10+
expect(at(11, 9).toString()).toEqual('11:09');
11+
});
12+
13+
xtest('midnight is zero hours', () => {
14+
expect(at(24, 0).toString()).toEqual('00:00');
15+
});
16+
17+
xtest('hour rolls over', () => {
18+
expect(at(25, 0).toString()).toEqual('01:00');
19+
});
20+
21+
xtest('hour rolls over continuously', () => {
22+
expect(at(100, 0).toString()).toEqual('04:00');
23+
});
24+
25+
xtest('sixty minutes is next hour', () => {
26+
expect(at(1, 60).toString()).toEqual('02:00');
27+
});
28+
29+
xtest('minutes roll over', () => {
30+
expect(at(0, 160).toString()).toEqual('02:40');
31+
});
32+
33+
xtest('minutes roll over continuously', () => {
34+
expect(at(0, 1723).toString()).toEqual('04:43');
35+
});
36+
37+
xtest('hour and minutes roll over', () => {
38+
expect(at(25, 160).toString()).toEqual('03:40');
39+
});
40+
41+
xtest('hour and minutes roll over continuously', () => {
42+
expect(at(201, 3001).toString()).toEqual('11:01');
43+
});
44+
45+
xtest('hour and minutes roll over to exactly midnight', () => {
46+
expect(at(72, 8640).toString()).toEqual('00:00');
47+
});
48+
49+
xtest('negative hour', () => {
50+
expect(at(-1, 15).toString()).toEqual('23:15');
51+
});
52+
53+
xtest('negative hour rolls over', () => {
54+
expect(at(-25, 0).toString()).toEqual('23:00');
55+
});
56+
57+
xtest('negative hour rolls over continuously', () => {
58+
expect(at(-91, 0).toString()).toEqual('05:00');
59+
});
60+
61+
xtest('negative minutes', () => {
62+
expect(at(1, -40).toString()).toEqual('00:20');
63+
});
64+
65+
xtest('negative minutes rolls over', () => {
66+
expect(at(1, -160).toString()).toEqual('22:20');
67+
});
68+
69+
xtest('negative minutes rolls over continuously', () => {
70+
expect(at(1, -4820).toString()).toEqual('16:40');
71+
});
72+
73+
xtest('negative hour and minutes both roll over', () => {
74+
expect(at(-25, -160).toString()).toEqual('20:20');
75+
});
76+
77+
xtest('negative hour and minutes both roll over continuously', () => {
78+
expect(at(-121, -5810).toString()).toEqual('22:10');
79+
});
80+
81+
describe('Adding and subtracting minutes', () => {
82+
xtest('add minutes', () => {
83+
expect(at(10, 0).plus(3).toString()).toEqual('10:03');
84+
});
85+
86+
xtest('add no minutes', () => {
87+
expect(at(6, 41).plus(0).toString()).toEqual('06:41');
88+
});
89+
90+
xtest('add to next hour', () => {
91+
expect(at(0, 45).plus(40).toString()).toEqual('01:25');
92+
});
93+
94+
xtest('add more than one hour', () => {
95+
expect(at(10, 0).plus(61).toString()).toEqual('11:01');
96+
});
97+
98+
xtest('add more than two hours with carry', () => {
99+
expect(at(0, 45).plus(160).toString()).toEqual('03:25');
100+
});
101+
102+
xtest('add across midnight', () => {
103+
expect(at(23, 59).plus(2).toString()).toEqual('00:01');
104+
});
105+
106+
xtest('add more than one day (1500 min = 25 hrs)', () => {
107+
expect(at(5, 32).plus(1500).toString()).toEqual('06:32');
108+
});
109+
110+
xtest('add more than two days', () => {
111+
expect(at(1, 1).plus(3500).toString()).toEqual('11:21');
112+
});
113+
114+
xtest('subtract minutes', () => {
115+
expect(at(10, 3).minus(3).toString()).toEqual('10:00');
116+
});
117+
118+
xtest('subtract to previous hour', () => {
119+
expect(at(10, 3).minus(30).toString()).toEqual('09:33');
120+
});
121+
122+
xtest('subtract more than an hour', () => {
123+
expect(at(10, 3).minus(70).toString()).toEqual('08:53');
124+
});
125+
126+
xtest('subtract across midnight', () => {
127+
expect(at(0, 3).minus(4).toString()).toEqual('23:59');
128+
});
129+
130+
xtest('subtract more than two hours', () => {
131+
expect(at(0, 0).minus(160).toString()).toEqual('21:20');
132+
});
133+
134+
xtest('subtract more than two hours with borrow', () => {
135+
expect(at(6, 15).minus(160).toString()).toEqual('03:35');
136+
});
137+
138+
xtest('subtract more than one day (1500 min = 25 hrs)', () => {
139+
expect(at(5, 32).minus(1500).toString()).toEqual('04:32');
140+
});
141+
142+
xtest('subtract more than two days', () => {
143+
expect(at(2, 20).minus(3000).toString()).toEqual('00:20');
144+
});
145+
});
146+
147+
describe('Construct two separate clocks, set times, test if they are equal', () => {
148+
xtest('clocks with same time', () => {
149+
expect(at(15, 37).equals(at(15, 37))).toBeTruthy();
150+
});
151+
152+
xtest('clocks a minute apart', () => {
153+
expect(at(15, 36).equals(at(15, 37))).toBeFalsy();
154+
});
155+
156+
xtest('clocks an hour apart', () => {
157+
expect(at(14, 37).equals(at(15, 37))).toBeFalsy();
158+
});
159+
160+
xtest('clocks with hour overflow', () => {
161+
expect(at(10, 37).equals(at(34, 37))).toBeTruthy();
162+
});
163+
164+
xtest('clocks with hour overflow by several days', () => {
165+
expect(at(3, 11).equals(at(99, 11))).toBeTruthy();
166+
});
167+
168+
xtest('clocks with negative hour', () => {
169+
expect(at(22, 40).equals(at(-2, 40))).toBeTruthy();
170+
});
171+
172+
xtest('clocks with negative hour that wraps', () => {
173+
expect(at(17, 3).equals(at(-31, 3))).toBeTruthy();
174+
});
175+
176+
xtest('clocks with negative hour that wraps multiple times', () => {
177+
expect(at(13, 49).equals(at(-83, 49))).toBeTruthy();
178+
});
179+
180+
xtest('clocks with minute overflow', () => {
181+
expect(at(0, 1).equals(at(0, 1441))).toBeTruthy();
182+
});
183+
184+
xtest('clocks with minute overflow by several days', () => {
185+
expect(at(2, 2).equals(at(2, 4322))).toBeTruthy();
186+
});
187+
188+
xtest('clocks with negative minute', () => {
189+
expect(at(2, 40).equals(at(3, -20))).toBeTruthy();
190+
});
191+
192+
xtest('clocks with negative minute that wraps', () => {
193+
expect(at(4, 10).equals(at(5, -1490))).toBeTruthy();
194+
});
195+
196+
xtest('clocks with negative minute that wraps multiple times', () => {
197+
expect(at(6, 15).equals(at(6, -4305))).toBeTruthy();
198+
});
199+
200+
xtest('clocks with negative hours and minutes', () => {
201+
expect(at(7, 32).equals(at(-12, -268))).toBeTruthy();
202+
});
203+
204+
xtest('clocks with negative hours and minutes that wrap', () => {
205+
expect(at(18, 7).equals(at(-54, -11513))).toBeTruthy();
206+
});
207+
});
208+
});
209+
});

clock/package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "exercism-javascript",
3+
"version": "0.0.0",
4+
"description": "Exercism exercises in Javascript.",
5+
"author": "Katrina Owen",
6+
"private": true,
7+
"repository": {
8+
"type": "git",
9+
"url": "https://github.com/exercism/javascript"
10+
},
11+
"devDependencies": {
12+
"@babel/cli": "^7.2.3",
13+
"@babel/core": "^7.4.0",
14+
"@babel/preset-env": "^7.4.2",
15+
"babel-eslint": "^10.0.1",
16+
"babel-jest": "^24.5.0",
17+
"eslint": "^5.15.3",
18+
"eslint-plugin-import": "^2.16.0",
19+
"jest": "^24.5.0"
20+
},
21+
"jest": {
22+
"modulePathIgnorePatterns": [
23+
"package.json"
24+
]
25+
},
26+
"scripts": {
27+
"test": "jest --no-cache ./*",
28+
"watch": "jest --no-cache --watch ./*",
29+
"lint": "eslint .",
30+
"lint-test": "eslint . && jest --no-cache ./* "
31+
},
32+
"license": "MIT",
33+
"dependencies": {}
34+
}

0 commit comments

Comments
 (0)