Skip to content

Commit d136f28

Browse files
committed
Write a test and change code to correctly convert midnight
1 parent 2fa8c14 commit d136f28

2 files changed

Lines changed: 6 additions & 1 deletion

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ function formatAs12HourClock(time) {
22
const hours = Number(time.slice(0, 2));
33
const minutes = time.slice(3, 5);
44

5-
if (hours > 12) {
5+
if (hours === 0) {
6+
return `12:${minutes} am`;
7+
} else if (hours > 12) {
68
const pmHours = (hours - 12).toString().padStart(2, "0");
79
return `${pmHours}:${minutes} pm`;
810
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ test("can correctly convert hours before midnight", () =>
1818

1919
test("can correctly convert hours before midday", () =>
2020
assert.equal(formatAs12HourClock("11:59"), "11:59 am"));
21+
22+
test("can correctly convert midnight", () =>
23+
assert.equal(formatAs12HourClock("00:00"), "12:00 am"));

0 commit comments

Comments
 (0)