Skip to content

Commit 65fc201

Browse files
added some corrections on the time converter test cases and the js function
1 parent 35054c4 commit 65fc201

2 files changed

Lines changed: 57 additions & 36 deletions

File tree

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
43
const minutes = time.slice(3, 5);
5-
const paddedPMHours =(hours - 12).toString().padStart(2, "0");
6-
const paddedAMHours = hours.toString().padStart(2, "0");
4+
const paddedPmHours = (hours - 12).toString().padStart(2, "0");
5+
const paddedAmHours = hours.toString().padStart(2, "0");
76

87
if (hours < 0 || hours > 24 || minutes < 0 || minutes > 59) {
98
return `Invalid time`;
109
}
11-
if (hours === 0) {
12-
return `12:${minutes} AM`;
13-
}
14-
if (hours === 12) {
15-
return `12:${minutes} PM`;
16-
}
17-
if (hours === 24) {
18-
return `12:${minutes} AM`;
19-
}
20-
if (hours > 12) {
21-
return `${paddedPMHours}:${minutes} PM`;
10+
if (hours === 0) {
11+
return `12:${minutes} am`;
12+
}
13+
if (hours === 12) {
14+
return `12:${minutes} pm`;
15+
}
16+
if (hours === 24) {
17+
return `12:${minutes} am`;
18+
}
19+
if (hours > 12) {
20+
return `${paddedPmHours}:${minutes} pm`;
2221
}
23-
return `${paddedAMHours}:${minutes} AM`;
22+
return `${paddedAmHours}:${minutes} am`;
2423
}
2524

26-
export {formatAs12HourClock};
25+
export { formatAs12HourClock };

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

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

5-
const cases = [
6-
["08:00", "08:00 AM"],
7-
["23:00", "11:00 PM"],
8-
["15:30", "03:30 PM"],
9-
["12:10", "12:10 PM"],
10-
["08:45", "08:45 AM"],
11-
["09:05", "09:05 AM"],
12-
["17:45", "05:45 PM"],
13-
["00:00", "12:00 AM"],
14-
["11:59", "11:59 AM"],
15-
["23:59", "11:59 PM"],
16-
];
17-
18-
cases.forEach(([input, expected]) => {
19-
const output = formatAs12HourClock(input);
20-
if (output === expected) {
21-
console.log(`✅ ${input} → returned "${output}"`);
22-
} else {
23-
console.error(`❌ ${input} → returned "${output}", expected "${expected}"`);
24-
}
5+
test("correctly convert 08:00 to morning time", function () {
6+
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
7+
});
8+
9+
test("correctly convert 23:00 to evening time", function () {
10+
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
11+
});
12+
13+
test("correctly convert 15:30 to afternoon time", function () {
14+
assert.equal(formatAs12HourClock("15:30"), "03:30 pm");
15+
});
16+
17+
test("correctly convert 12:10 to 10 past noon time", function () {
18+
assert.equal(formatAs12HourClock("12:10"), "12:10 pm");
19+
});
20+
21+
test("correctly convert 12:00 to exactly noon time", function () {
22+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
23+
});
24+
25+
test("correctly convert 08:45 to morning time", function () {
26+
assert.equal(formatAs12HourClock("08:45"), "08:45 am");
27+
});
28+
29+
test("correctly convert 09:05 to morning time", function () {
30+
assert.equal(formatAs12HourClock("09:05"), "09:05 am");
31+
});
32+
33+
test("correctly convert 17:45 to evening time", function () {
34+
assert.equal(formatAs12HourClock("17:45"), "05:45 pm");
35+
});
36+
37+
test("correctly convert midnight 00:00", function () {
38+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
39+
});
40+
41+
test("correctly convert 11:59 to late morning time", function () {
42+
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
43+
});
44+
45+
test("correctly convert 23:59 to late night time", function () {
46+
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
2547
});

0 commit comments

Comments
 (0)