Skip to content

Commit fc0d336

Browse files
correcting Errors and reorgnising code
1 parent 3e6d6c8 commit fc0d336

2 files changed

Lines changed: 58 additions & 43 deletions

File tree

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,70 @@
11
function formatAs12HourClock(time) {
2-
if (time.length == 4) {
3-
c;
4-
time = time.padStart(5, "0");
5-
}
2+
if (typeof time !== "string") {
3+
return "Incorrect timing format";
4+
}
5+
const correctTimingFormat = time.match(/^(\d{1,2}):(\d{2})$/);
66

7-
if (typeof time !== "string") {
8-
return "nccorect timing format";
7+
if (!correctTimingFormat) {
8+
return "Incorrect timing format";
99
}
1010

11-
const match = time.match(/^(\d{1,2}):(\d{2})$/);
11+
if (time.length == 4) {
12+
time = time.padStart(5, "0");
13+
}
1214

13-
if (!match) {
14-
return "Inccorect timing format";
15-
}
15+
const match = time.match(/^(\d{1,2}):(\d{2})$/);
1616

17-
const hours = Number(time.slice(0, 2));
18-
const minutes = Number(time.slice(3, 5));
17+
if (!match) {
18+
return "Incorrect timing format";
19+
}
1920

20-
if (time === "12:00") {
21-
return `12:00 pm`;
22-
}
21+
const hours = Number(time.slice(0, 2));
22+
const minutes = Number(time.slice(3, 5));
2323

24-
if (hours < 12) {
25-
return `${time} am`;
26-
}
27-
if (hours >= 12) {
28-
return `${time} pm`;
29-
}
30-
if (hours == 24) {
31-
return `12:00 am`;
32-
}
24+
// 00:00
25+
if (time === "00:00") {
26+
return `12:00 am`;
27+
}
3328

34-
if (hours > 12 && hours < 22 && minutes < 10) {
35-
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
36-
}
29+
// 12:00
30+
if (time === "12:00") {
31+
return `12:00 pm`;
32+
}
3733

38-
if (hours > 12 && hours < 22 && minutes < 10) {
39-
return `${(hours - 12).toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")} pm`;
40-
}
41-
if (hours > 12 && hours < 22 && minutes >= 10) {
42-
return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`;
43-
}
44-
if (hours >= 22 && minutes < 10) {
45-
return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`;
46-
}
47-
if (hours >= 22 && minutes >= 10) {
48-
return `${hours - 12}:${minutes} pm`;
49-
}
50-
if (hours >= 12 && minutes >= 10) {
51-
return `${hours}:${minutes} pm`;
52-
}
34+
// 00:xx
35+
if (hours === 0) {
36+
return `12:${minutes.toString().padStart(2, "0")} am`;
37+
}
38+
39+
// 01:xx - 11:xx
40+
if (hours < 12) {
41+
return `${time} am`;
42+
}
43+
44+
// 12:xx
45+
if (hours === 12) {
46+
return `${time} pm`;
47+
}
48+
49+
// 13:00 - 21:59
50+
if (hours > 12 && hours < 22 && minutes < 10) {
51+
return `${(hours - 12).toString().padStart(2, "0")}:${minutes
52+
.toString()
53+
.padStart(2, "0")} pm`;
54+
}
55+
56+
if (hours > 12 && hours < 22 && minutes >= 10) {
57+
return `${(hours - 12).toString().padStart(2, "0")}:${minutes} pm`;
58+
}
59+
60+
// 22:00 - 23:59
61+
if (hours >= 22 && minutes < 10) {
62+
return `${hours - 12}:${minutes.toString().padStart(2, "0")} pm`;
63+
}
64+
65+
if (hours >= 22 && minutes >= 10) {
66+
return `${hours - 12}:${minutes} pm`;
67+
}
5368
}
5469

5570
export { formatAs12HourClock };

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ test("can correctly convert time with missing leading zero", function () {
2424
});
2525

2626
test("can correctly handel non digit format entries", function () {
27-
assert.equal(formatAs12HourClock("nineOclock"), "Inccorect timing format");
27+
assert.equal(formatAs12HourClock("nineOclock"), "Incorrect timing format");
2828
});
2929
/* ************************ Edge cases Minutes *****************************************************/
3030
/* over one hour <- 60 30 0 -> negative number */

0 commit comments

Comments
 (0)