Skip to content

Commit 3e6d6c8

Browse files
fixing errors
1 parent 556f283 commit 3e6d6c8

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
function formatAs12HourClock(time) {
2-
console.log(time.length);
3-
42
if (time.length == 4) {
5-
console.log("got here");
3+
c;
64
time = time.padStart(5, "0");
75
}
86

@@ -19,21 +17,24 @@ function formatAs12HourClock(time) {
1917
const hours = Number(time.slice(0, 2));
2018
const minutes = Number(time.slice(3, 5));
2119

22-
if (time === "00:00") {
23-
return `12:00 am`;
24-
}
2520
if (time === "12:00") {
2621
return `12:00 pm`;
2722
}
2823

2924
if (hours < 12) {
3025
return `${time} am`;
31-
} else if (hours == 12) {
32-
return `${time} am`;
33-
} else if (hours == 24) {
26+
}
27+
if (hours >= 12) {
28+
return `${time} pm`;
29+
}
30+
if (hours == 24) {
3431
return `12:00 am`;
3532
}
3633

34+
if (hours > 12 && hours < 22 && minutes < 10) {
35+
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
36+
}
37+
3738
if (hours > 12 && hours < 22 && minutes < 10) {
3839
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
3940
}
@@ -46,6 +47,9 @@ function formatAs12HourClock(time) {
4647
if (hours >= 22 && minutes >= 10) {
4748
return `${hours - 12}:${minutes} pm`;
4849
}
50+
if (hours >= 12 && minutes >= 10) {
51+
return `${hours}:${minutes} pm`;
52+
}
4953
}
5054

5155
export { formatAs12HourClock };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test("can corectly convert mid-day", () =>
1717
assert.equal(formatAs12HourClock("12:00"), "12:00 pm"));
1818

1919
test("can corectly convert half an hour passed midnight", () =>
20-
assert.equal(formatAs12HourClock("00:30"), "00:30 am"));
20+
assert.equal(formatAs12HourClock("00:30"), "12:30 am"));
2121

2222
test("can correctly convert time with missing leading zero", function () {
2323
assert.equal(formatAs12HourClock("9:00"), "09:00 am");

0 commit comments

Comments
 (0)