Skip to content

Commit 650452e

Browse files
committed
Fix format clock edge cases
1 parent 29ef74a commit 650452e

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
function formatAs12HourClock(time) {
22

33
const hours = Number(time.slice(0, 2));
4+
const minutes = time.slice(3);
5+
6+
if (hours === 0) {
7+
return `12:${minutes} am`;
8+
}
9+
10+
if (hours === 12) {
11+
return `${time} pm`;
12+
}
413

514
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
15+
return `${hours - 12}:${minutes} pm`;
716
}
17+
818
return `${time} am`;
919
}
1020

11-
export {formatAs12HourClock};
21+
export {formatAs12HourClock};

‎format-clock-edge-cases/timeConverter.test.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,25 @@ test("correctly convert time after 12:00", function(){
99
test("can correctly convert morning time", function() {
1010
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
1111
});
12+
13+
test("correctly convert midnight", function() {
14+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
15+
});
16+
test("correctly convert noon", function() {
17+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
18+
});
19+
test("correctly convert 13:00", function() {
20+
assert.equal(formatAs12HourClock("13:00"), "1:00 pm");
21+
});
22+
test("correctly keep the minutes", function() {
23+
assert.equal(formatAs12HourClock("13:45"), "1:45 pm");
24+
});
25+
test("correctly convert 12:30", function() {
26+
assert.equal(formatAs12HourClock("12:30"), "12:30 pm");
27+
});
28+
test("correctly convert the last minute of the day", function() {
29+
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
30+
});
31+
test("correctly convert 00:30", function() {
32+
assert.equal(formatAs12HourClock("00:30"), "12:30 am");
33+
});

0 commit comments

Comments
 (0)