Skip to content

Commit f54872d

Browse files
committed
edge cases
1 parent 29ef74a commit f54872d

2 files changed

Lines changed: 25 additions & 9 deletions

File tree

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
function formatAs12HourClock(time) {
2+
let hours = Number(time.slice(0, 2));
3+
const minutes = time.slice(3);
24

3-
const hours = Number(time.slice(0, 2));
5+
const period = hours >= 12 ? "pm" : "am";
46

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
7-
}
8-
return `${time} am`;
7+
if (hours === 0) {
8+
hours = 12;
9+
} else if (hours > 12) {
10+
hours -= 12;
11+
}
12+
13+
const formattedHours =
14+
hours < 10 && hours !== 12 ? `0${hours}` : hours;
15+
16+
return `${formattedHours}:${minutes} ${period}`;
917
}
1018

11-
export {formatAs12HourClock};
19+
export { formatAs12HourClock };
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
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(){
5+
test("correctly convert time after 12:00", function () {
66
assert.equal(formatAs12HourClock("23:00"), "11:00 pm");
77
});
88

9-
test("can correctly convert morning time", function() {
9+
test("can correctly convert morning time", function () {
1010
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
1111
});
12+
13+
test("converts midnight (00:00)", function () {
14+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
15+
});
16+
17+
test("converts noon (12:00)", function () {
18+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
19+
});

0 commit comments

Comments
 (0)