Skip to content

Commit ee44992

Browse files
committed
Fix 12-hour clock conversion and add edge case tests
1 parent 29ef74a commit ee44992

2 files changed

Lines changed: 73 additions & 12 deletions

File tree

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1-
function formatAs12HourClock(time) {
2-
1+
export function formatAs12HourClock(time) {
32
const hours = Number(time.slice(0, 2));
3+
const minutes = time.slice(2);
4+
5+
const period = hours >= 12 ? "pm" : "am";
6+
7+
let hours12 = hours % 12;
48

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
9+
if (hours12 === 0) {
10+
hours12 = 12;
711
}
8-
return `${time} am`;
9-
}
1012

11-
export {formatAs12HourClock};
13+
const formattedHours = String(hours12).padStart(2, "0");
14+
15+
return `${formattedHours}${minutes} ${period}`;
16+
}
Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,67 @@
1-
import {formatAs12HourClock} from "./timeConverter.js";
1+
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(){
6-
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
5+
test("correctly convert time after 12:00", function () {
6+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
77
});
88

9-
test("can correctly convert morning time", function() {
10-
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
9+
test("can correctly convert morning time", function () {
10+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
11+
});
12+
13+
test("converts midnight correctly", function () {
14+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
15+
});
16+
17+
test("converts one minute after midnight", function () {
18+
assert.equal(formatAs12HourClock("00:01"), "12:01 am");
19+
});
20+
21+
test("converts early morning time", function () {
22+
assert.equal(formatAs12HourClock("01:30"), "01:30 am");
23+
});
24+
25+
test("converts last minute before noon", function () {
26+
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
27+
});
28+
29+
test("converts noon correctly", function () {
30+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
31+
});
32+
33+
test("converts one minute after noon", function () {
34+
assert.equal(formatAs12HourClock("12:01"), "12:01 pm");
35+
});
36+
37+
test("converts afternoon time", function () {
38+
assert.equal(formatAs12HourClock("13:30"), "01:30 pm");
39+
});
40+
41+
test("converts evening time", function () {
42+
assert.equal(formatAs12HourClock("18:45"), "06:45 pm");
43+
});
44+
45+
test("converts last minute of the day", function () {
46+
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
47+
});
48+
49+
test("converts 09:59 correctly", function () {
50+
assert.equal(formatAs12HourClock("09:59"), "09:59 am");
51+
});
52+
53+
test("converts 10:00 correctly", function () {
54+
assert.equal(formatAs12HourClock("10:00"), "10:00 am");
55+
});
56+
57+
test("converts 12:59 correctly", function () {
58+
assert.equal(formatAs12HourClock("12:59"), "12:59 pm");
59+
});
60+
61+
test("converts 13:00 correctly", function () {
62+
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
63+
});
64+
65+
test("converts 23:00 correctly", function () {
66+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
1167
});

0 commit comments

Comments
 (0)