Skip to content

Commit b98a9fb

Browse files
committed
Fixed bugs and added more test cases
1 parent 29ef74a commit b98a9fb

2 files changed

Lines changed: 32 additions & 6 deletions

File tree

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
3+
const minutes = time.slice(-2);
4+
let hours12;
5+
let timePeriod;
46

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
7+
if (hours === 0) {
8+
hours12 = "12";
9+
timePeriod = "am";
10+
}
11+
else if (hours === 12) {
12+
hours12 = "12";
13+
timePeriod = "pm";
14+
}
15+
else if (hours < 12) {
16+
hours12 = String(hours12).padStart(2, "0");
17+
timePeriod = "am";
718
}
8-
return `${time} am`;
19+
else if (hours > 12) {
20+
hours12 = String(hours12 - 12).padStart(2, "0");
21+
timePeriod = "pm"
22+
}
23+
return `${hours12}:${minutes} ${timePeriod}`
924
}
10-
11-
export {formatAs12HourClock};
25+
export {formatAs12HourClock};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,15 @@ test("correctly convert time after 12:00", function(){
99
test("can correctly convert morning time", function() {
1010
assert.equal(formatAs12HourClock("08:00"), "08:00 am");
1111
});
12+
13+
test("can correctly convert midnight", function () {
14+
assert.equal(formatAs12HourClock("00:00"), "12:00 am");
15+
});
16+
17+
test("can correctly convert noon", function () {
18+
assert.equal(formatAs12HourClock("12:00"), "12:00 pm");
19+
});
20+
21+
test("can correctly convert time with minutes", function () {
22+
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
23+
});

0 commit comments

Comments
 (0)