Skip to content

Commit 2ec464d

Browse files
Refactor time conversion logic and enhance test coverage for edge cases
1 parent 29ef74a commit 2ec464d

2 files changed

Lines changed: 54 additions & 4 deletions

File tree

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

33
const hours = Number(time.slice(0, 2));
4+
const minutes = time.slice(2);
5+
6+
if (hours === 24) {
7+
return `12${minutes} am`
8+
}
49

510
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
11+
return `${hours - 12}${minutes} pm`;
712
}
13+
814
return `${time} am`;
915
}
1016

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

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,54 @@ import {formatAs12HourClock} from "./timeConverter.js";
22
import assert from "node:assert";
33
import test from "node:test";
44

5-
test("correctly convert time after 12:00", function(){
5+
test("correctly convert hours after 12:00", () => {
66
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
77
});
88

9-
test("can correctly convert morning time", function() {
9+
test("correctly convert minutes after 12:00", () => {
10+
assert.equal(formatAs12HourClock("23:53"), "11:53 pm");
11+
});
12+
13+
test("can correctly convert morning time", () => {
1014
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
1115
});
16+
17+
test("can correctly convert morning time minutes", () => {
18+
assert.equal(formatAs12HourClock("08:30"), "08:30 am");
19+
});
20+
21+
test("can correctly convert morning time minutes", () => {
22+
assert.equal(formatAs12HourClock("08:34"), "08:34 am");
23+
});
24+
25+
test("can successfully convert midnight time", () =>
26+
assert.equal(formatAs12HourClock("24:00"), "12:00 am"));
27+
28+
29+
30+
/*
31+
Edge cases:
32+
Correctly convert hours after 12:00
33+
Input | Expected
34+
23:00 11:00 pm
35+
36+
Correctly convert afternoon hours and minutes
37+
Input | Expected
38+
23:00 11:53 pm
39+
40+
Correctly convert morning time and minutes
41+
Input | Expected
42+
08:00 08:00 am
43+
44+
Correctly convert morning time and minutes
45+
Input | Expected
46+
08:30 08:30 am
47+
48+
Correctly convert morning time and minutes
49+
Input | Expected
50+
08:34 08:34 am
51+
52+
Correctly convert midnight time
53+
Input | Expected
54+
24:00 12:00 am
55+
*/

0 commit comments

Comments
 (0)