Skip to content

Commit e6a462c

Browse files
committed
updated files
1 parent f8a6c20 commit e6a462c

2 files changed

Lines changed: 17 additions & 11 deletions

File tree

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
function formatAs12HourClock(time) {
22
const hours = Number(time.slice(0, 2));
3-
const minutes = time.slice(-2);
3+
const minutes = time.slice(3, 5);
44

5-
if (time == "00") {
6-
return `12:00:${minutes} am`;
5+
if (hours == "00") {
6+
return `12:${minutes} am`;
77
} else if (hours > 12) {
8-
return `${hours - 12}:${minutes} pm`;
8+
return `${String(hours - 12).padStart(2, "0")}:${minutes} pm`;
99
} else if (hours === 12) {
1010
return `12:${minutes} pm`;
1111
} else {
1212
return `${time} am`;
1313
}
1414
}
1515

16-
console.log(formatAs12HourClock("23:00"));
17-
console.log(formatAs12HourClock("14:00"));
18-
console.log(formatAs12HourClock("12:00"));
19-
console.log(formatAs12HourClock("08:15"));
20-
console.log(formatAs12HourClock("00:00"));
21-
2216
export { formatAs12HourClock };

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ test("can correctly convert 30 minutes before 12:00", function () {
2727
});
2828

2929
test("can correctly convert time at the beginning of noon", function () {
30-
assert.equal(formatAs12HourClock("12:001"), "12:01 pm");
30+
assert.equal(formatAs12HourClock("12:01"), "12:01 pm");
3131
});
3232

3333
test("can correctly convert half hour before midnight", function () {
@@ -37,3 +37,15 @@ test("can correctly convert half hour before midnight", function () {
3737
test("can correctly convert 1 minute before midnight", function () {
3838
assert.equal(formatAs12HourClock("23:59"), "11:59 pm");
3939
});
40+
41+
test("can correctly convert half past midnight", function () {
42+
assert.equal(formatAs12HourClock("00:30"), "12:30 am");
43+
});
44+
45+
test("can correctly convert 1 minute before midday", function () {
46+
assert.equal(formatAs12HourClock("11:59"), "11:59 am");
47+
});
48+
49+
test("can correctly convert 1 hour after midday", function () {
50+
assert.equal(formatAs12HourClock("13:00"), "01:00 pm");
51+
});

0 commit comments

Comments
 (0)