Skip to content

Commit 9f548e1

Browse files
author
programmiri
committed
Solve exercise clock
1 parent 46769f2 commit 9f548e1

File tree

2 files changed

+157
-65
lines changed

2 files changed

+157
-65
lines changed

clock/clock.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function dateToString(date) {
2+
return date.toTimeString().slice(0, 5);
3+
}
4+
5+
export function at(hour = 0, minutes = 0) {
6+
const baseDate = new Date();
7+
baseDate.setHours(hour, minutes);
8+
9+
return {
10+
toString: function() {
11+
return dateToString(baseDate);
12+
},
13+
14+
plus: function(minToAdd) {
15+
baseDate.setMinutes(minToAdd + minutes);
16+
return dateToString(baseDate);
17+
},
18+
19+
minus: function(minToSub) {
20+
baseDate.setMinutes(minutes - minToSub);
21+
return dateToString(baseDate);
22+
},
23+
24+
equals: function(clock) {
25+
return dateToString(baseDate) == clock.toString();
26+
},
27+
};
28+
}

clock/clock.spec.js

Lines changed: 129 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,202 +6,266 @@ describe('Clock', () => {
66
expect(at(8).toString()).toEqual('08:00');
77
});
88

9-
xtest('past the hour', () => {
9+
test('past the hour', () => {
1010
expect(at(11, 9).toString()).toEqual('11:09');
1111
});
1212

13-
xtest('midnight is zero hours', () => {
13+
test('midnight is zero hours', () => {
1414
expect(at(24, 0).toString()).toEqual('00:00');
1515
});
1616

17-
xtest('hour rolls over', () => {
17+
test('hour rolls over', () => {
1818
expect(at(25, 0).toString()).toEqual('01:00');
1919
});
2020

21-
xtest('hour rolls over continuously', () => {
21+
test('hour rolls over continuously', () => {
2222
expect(at(100, 0).toString()).toEqual('04:00');
2323
});
2424

25-
xtest('sixty minutes is next hour', () => {
25+
test('sixty minutes is next hour', () => {
2626
expect(at(1, 60).toString()).toEqual('02:00');
2727
});
2828

29-
xtest('minutes roll over', () => {
29+
test('minutes roll over', () => {
3030
expect(at(0, 160).toString()).toEqual('02:40');
3131
});
3232

33-
xtest('minutes roll over continuously', () => {
33+
test('minutes roll over continuously', () => {
3434
expect(at(0, 1723).toString()).toEqual('04:43');
3535
});
3636

37-
xtest('hour and minutes roll over', () => {
37+
test('hour and minutes roll over', () => {
3838
expect(at(25, 160).toString()).toEqual('03:40');
3939
});
4040

41-
xtest('hour and minutes roll over continuously', () => {
41+
test('hour and minutes roll over continuously', () => {
4242
expect(at(201, 3001).toString()).toEqual('11:01');
4343
});
4444

45-
xtest('hour and minutes roll over to exactly midnight', () => {
45+
test('hour and minutes roll over to exactly midnight', () => {
4646
expect(at(72, 8640).toString()).toEqual('00:00');
4747
});
4848

49-
xtest('negative hour', () => {
49+
test('negative hour', () => {
5050
expect(at(-1, 15).toString()).toEqual('23:15');
5151
});
5252

53-
xtest('negative hour rolls over', () => {
53+
test('negative hour rolls over', () => {
5454
expect(at(-25, 0).toString()).toEqual('23:00');
5555
});
5656

57-
xtest('negative hour rolls over continuously', () => {
57+
test('negative hour rolls over continuously', () => {
5858
expect(at(-91, 0).toString()).toEqual('05:00');
5959
});
6060

61-
xtest('negative minutes', () => {
61+
test('negative minutes', () => {
6262
expect(at(1, -40).toString()).toEqual('00:20');
6363
});
6464

65-
xtest('negative minutes rolls over', () => {
65+
test('negative minutes rolls over', () => {
6666
expect(at(1, -160).toString()).toEqual('22:20');
6767
});
6868

69-
xtest('negative minutes rolls over continuously', () => {
69+
test('negative minutes rolls over continuously', () => {
7070
expect(at(1, -4820).toString()).toEqual('16:40');
7171
});
7272

73-
xtest('negative hour and minutes both roll over', () => {
73+
test('negative hour and minutes both roll over', () => {
7474
expect(at(-25, -160).toString()).toEqual('20:20');
7575
});
7676

77-
xtest('negative hour and minutes both roll over continuously', () => {
77+
test('negative hour and minutes both roll over continuously', () => {
7878
expect(at(-121, -5810).toString()).toEqual('22:10');
7979
});
8080

8181
describe('Adding and subtracting minutes', () => {
82-
xtest('add minutes', () => {
83-
expect(at(10, 0).plus(3).toString()).toEqual('10:03');
82+
test('add minutes', () => {
83+
expect(
84+
at(10, 0)
85+
.plus(3)
86+
.toString()
87+
).toEqual('10:03');
8488
});
8589

86-
xtest('add no minutes', () => {
87-
expect(at(6, 41).plus(0).toString()).toEqual('06:41');
90+
test('add no minutes', () => {
91+
expect(
92+
at(6, 41)
93+
.plus(0)
94+
.toString()
95+
).toEqual('06:41');
8896
});
8997

90-
xtest('add to next hour', () => {
91-
expect(at(0, 45).plus(40).toString()).toEqual('01:25');
98+
test('add to next hour', () => {
99+
expect(
100+
at(0, 45)
101+
.plus(40)
102+
.toString()
103+
).toEqual('01:25');
92104
});
93105

94-
xtest('add more than one hour', () => {
95-
expect(at(10, 0).plus(61).toString()).toEqual('11:01');
106+
test('add more than one hour', () => {
107+
expect(
108+
at(10, 0)
109+
.plus(61)
110+
.toString()
111+
).toEqual('11:01');
96112
});
97113

98-
xtest('add more than two hours with carry', () => {
99-
expect(at(0, 45).plus(160).toString()).toEqual('03:25');
114+
test('add more than two hours with carry', () => {
115+
expect(
116+
at(0, 45)
117+
.plus(160)
118+
.toString()
119+
).toEqual('03:25');
100120
});
101121

102-
xtest('add across midnight', () => {
103-
expect(at(23, 59).plus(2).toString()).toEqual('00:01');
122+
test('add across midnight', () => {
123+
expect(
124+
at(23, 59)
125+
.plus(2)
126+
.toString()
127+
).toEqual('00:01');
104128
});
105129

106-
xtest('add more than one day (1500 min = 25 hrs)', () => {
107-
expect(at(5, 32).plus(1500).toString()).toEqual('06:32');
130+
test('add more than one day (1500 min = 25 hrs)', () => {
131+
expect(
132+
at(5, 32)
133+
.plus(1500)
134+
.toString()
135+
).toEqual('06:32');
108136
});
109137

110-
xtest('add more than two days', () => {
111-
expect(at(1, 1).plus(3500).toString()).toEqual('11:21');
138+
test('add more than two days', () => {
139+
expect(
140+
at(1, 1)
141+
.plus(3500)
142+
.toString()
143+
).toEqual('11:21');
112144
});
113145

114-
xtest('subtract minutes', () => {
115-
expect(at(10, 3).minus(3).toString()).toEqual('10:00');
146+
test('subtract minutes', () => {
147+
expect(
148+
at(10, 3)
149+
.minus(3)
150+
.toString()
151+
).toEqual('10:00');
116152
});
117153

118-
xtest('subtract to previous hour', () => {
119-
expect(at(10, 3).minus(30).toString()).toEqual('09:33');
154+
test('subtract to previous hour', () => {
155+
expect(
156+
at(10, 3)
157+
.minus(30)
158+
.toString()
159+
).toEqual('09:33');
120160
});
121161

122-
xtest('subtract more than an hour', () => {
123-
expect(at(10, 3).minus(70).toString()).toEqual('08:53');
162+
test('subtract more than an hour', () => {
163+
expect(
164+
at(10, 3)
165+
.minus(70)
166+
.toString()
167+
).toEqual('08:53');
124168
});
125169

126-
xtest('subtract across midnight', () => {
127-
expect(at(0, 3).minus(4).toString()).toEqual('23:59');
170+
test('subtract across midnight', () => {
171+
expect(
172+
at(0, 3)
173+
.minus(4)
174+
.toString()
175+
).toEqual('23:59');
128176
});
129177

130-
xtest('subtract more than two hours', () => {
131-
expect(at(0, 0).minus(160).toString()).toEqual('21:20');
178+
test('subtract more than two hours', () => {
179+
expect(
180+
at(0, 0)
181+
.minus(160)
182+
.toString()
183+
).toEqual('21:20');
132184
});
133185

134-
xtest('subtract more than two hours with borrow', () => {
135-
expect(at(6, 15).minus(160).toString()).toEqual('03:35');
186+
test('subtract more than two hours with borrow', () => {
187+
expect(
188+
at(6, 15)
189+
.minus(160)
190+
.toString()
191+
).toEqual('03:35');
136192
});
137193

138-
xtest('subtract more than one day (1500 min = 25 hrs)', () => {
139-
expect(at(5, 32).minus(1500).toString()).toEqual('04:32');
194+
test('subtract more than one day (1500 min = 25 hrs)', () => {
195+
expect(
196+
at(5, 32)
197+
.minus(1500)
198+
.toString()
199+
).toEqual('04:32');
140200
});
141201

142-
xtest('subtract more than two days', () => {
143-
expect(at(2, 20).minus(3000).toString()).toEqual('00:20');
202+
test('subtract more than two days', () => {
203+
expect(
204+
at(2, 20)
205+
.minus(3000)
206+
.toString()
207+
).toEqual('00:20');
144208
});
145209
});
146210

147211
describe('Construct two separate clocks, set times, test if they are equal', () => {
148-
xtest('clocks with same time', () => {
212+
test('clocks with same time', () => {
149213
expect(at(15, 37).equals(at(15, 37))).toBeTruthy();
150214
});
151215

152-
xtest('clocks a minute apart', () => {
216+
test('clocks a minute apart', () => {
153217
expect(at(15, 36).equals(at(15, 37))).toBeFalsy();
154218
});
155219

156-
xtest('clocks an hour apart', () => {
220+
test('clocks an hour apart', () => {
157221
expect(at(14, 37).equals(at(15, 37))).toBeFalsy();
158222
});
159223

160-
xtest('clocks with hour overflow', () => {
224+
test('clocks with hour overflow', () => {
161225
expect(at(10, 37).equals(at(34, 37))).toBeTruthy();
162226
});
163227

164-
xtest('clocks with hour overflow by several days', () => {
228+
test('clocks with hour overflow by several days', () => {
165229
expect(at(3, 11).equals(at(99, 11))).toBeTruthy();
166230
});
167231

168-
xtest('clocks with negative hour', () => {
232+
test('clocks with negative hour', () => {
169233
expect(at(22, 40).equals(at(-2, 40))).toBeTruthy();
170234
});
171235

172-
xtest('clocks with negative hour that wraps', () => {
236+
test('clocks with negative hour that wraps', () => {
173237
expect(at(17, 3).equals(at(-31, 3))).toBeTruthy();
174238
});
175239

176-
xtest('clocks with negative hour that wraps multiple times', () => {
240+
test('clocks with negative hour that wraps multiple times', () => {
177241
expect(at(13, 49).equals(at(-83, 49))).toBeTruthy();
178242
});
179243

180-
xtest('clocks with minute overflow', () => {
244+
test('clocks with minute overflow', () => {
181245
expect(at(0, 1).equals(at(0, 1441))).toBeTruthy();
182246
});
183247

184-
xtest('clocks with minute overflow by several days', () => {
248+
test('clocks with minute overflow by several days', () => {
185249
expect(at(2, 2).equals(at(2, 4322))).toBeTruthy();
186250
});
187251

188-
xtest('clocks with negative minute', () => {
252+
test('clocks with negative minute', () => {
189253
expect(at(2, 40).equals(at(3, -20))).toBeTruthy();
190254
});
191255

192-
xtest('clocks with negative minute that wraps', () => {
256+
test('clocks with negative minute that wraps', () => {
193257
expect(at(4, 10).equals(at(5, -1490))).toBeTruthy();
194258
});
195259

196-
xtest('clocks with negative minute that wraps multiple times', () => {
260+
test('clocks with negative minute that wraps multiple times', () => {
197261
expect(at(6, 15).equals(at(6, -4305))).toBeTruthy();
198262
});
199263

200-
xtest('clocks with negative hours and minutes', () => {
264+
test('clocks with negative hours and minutes', () => {
201265
expect(at(7, 32).equals(at(-12, -268))).toBeTruthy();
202266
});
203267

204-
xtest('clocks with negative hours and minutes that wrap', () => {
268+
test('clocks with negative hours and minutes that wrap', () => {
205269
expect(at(18, 7).equals(at(-54, -11513))).toBeTruthy();
206270
});
207271
});

0 commit comments

Comments
 (0)