Skip to content

Commit 5733d12

Browse files
committed
In 5-stretch-extend/format-time.js, tried edge cases and invalid inputs
1 parent 401db2b commit 5733d12

File tree

1 file changed

+109
-1
lines changed

1 file changed

+109
-1
lines changed

Sprint-2/5-stretch-extend/format-time.js

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,38 @@
44

55
function formatAs12HourClock(time) {
66
const hours = Number(time.slice(0, 2));
7+
const minutes = Number(time.slice(3));
8+
if (isNaN(hours) || isNaN(minutes)) {
9+
throw new Error("Invalid time");
10+
}
11+
if (hours < 0 || hours > 24 || minutes < 0 || minutes > 59) {
12+
throw new Error("Invalid time");
13+
}
14+
if (time[2] !== ":") {
15+
throw new Error("Invalid time");
16+
}
17+
if (time.length !== 5) {
18+
throw new Error("Invalid time");
19+
}
20+
21+
const minutesStr = String(minutes).padEnd(2, 0);
22+
23+
if (hours == 0) {
24+
return `12:${minutesStr} am`;
25+
}
26+
27+
if (hours == 12 && minutes >= 0) {
28+
return `12:${minutesStr} pm`;
29+
}
30+
31+
if (hours == 24) {
32+
return `12:${minutesStr} am`;
33+
}
34+
735
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
36+
return `${String(hours - 12).padStart(2, 0)}:${minutesStr} pm`;
937
}
38+
1039
return `${time} am`;
1140
}
1241

@@ -23,3 +52,82 @@ console.assert(
2352
currentOutput2 === targetOutput2,
2453
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2554
);
55+
56+
const currentOutput3 = formatAs12HourClock("12:00");
57+
const targetOutput3 = "12:00 pm";
58+
console.assert(
59+
currentOutput3 === targetOutput3,
60+
`current output: ${currentOutput3}, target output: ${targetOutput3}`
61+
);
62+
63+
const currentOutput4 = formatAs12HourClock("00:00");
64+
const targetOutput4 = "12:00 am";
65+
console.assert(
66+
currentOutput4 === targetOutput4,
67+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
68+
);
69+
70+
const currentOutput5 = formatAs12HourClock("15:30");
71+
const targetOutput5 = "03:30 pm";
72+
console.assert(
73+
currentOutput5 === targetOutput5,
74+
`current output: ${currentOutput5}, target output: ${targetOutput5}`
75+
);
76+
77+
const currentOutput6 = formatAs12HourClock("11:15");
78+
const targetOutput6 = "11:15 am";
79+
console.assert(
80+
currentOutput6 === targetOutput6,
81+
`current output: ${currentOutput6}, target output: ${targetOutput6}`
82+
);
83+
84+
const currentOutput7 = formatAs12HourClock("00:30");
85+
const targetOutput7 = "12:30 am";
86+
console.assert(
87+
currentOutput7 === targetOutput7,
88+
`current output: ${currentOutput7}, target output: ${targetOutput7}`
89+
);
90+
91+
const currentOutput8 = formatAs12HourClock("12:45");
92+
const targetOutput8 = "12:45 pm";
93+
console.assert(
94+
currentOutput8 === targetOutput8,
95+
`current output: ${currentOutput8}, target output: ${targetOutput8}`
96+
);
97+
98+
const currentOutput9 = formatAs12HourClock("24:00");
99+
const targetOutput9 = "12:00 am";
100+
console.assert(
101+
currentOutput9 === targetOutput9,
102+
`current output: ${currentOutput9}, target output: ${targetOutput9}`
103+
);
104+
105+
try {
106+
formatAs12HourClock("25:00");
107+
console.assert(false, "Expected an error for invalid hours");
108+
} catch (e) {
109+
console.assert(
110+
e.message === "Invalid time",
111+
`Unexpected error message: ${e.message}`
112+
);
113+
}
114+
115+
try {
116+
formatAs12HourClock("10:60");
117+
console.assert(false, "Expected an error for invalid minutes");
118+
} catch (e) {
119+
console.assert(
120+
e.message === "Invalid time",
121+
`Unexpected error message: ${e.message}`
122+
);
123+
}
124+
125+
try {
126+
formatAs12HourClock("ab:cd");
127+
console.assert(false, "Expected an error for non-numeric input");
128+
} catch (e) {
129+
console.assert(
130+
e.message === "Invalid time",
131+
`Unexpected error message: ${e.message}`
132+
);
133+
}

0 commit comments

Comments
 (0)