Skip to content

Commit 3ae7c4f

Browse files
make a function which run all the test cases
1 parent 96b65cf commit 3ae7c4f

1 file changed

Lines changed: 24 additions & 4 deletions

File tree

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1+
// This code only work when there is only hours not whenn there is minutes.
2+
// function formatAs12HourClock(time) {
3+
// const hours = Number(time.slice(0, 2));
4+
5+
// if (time === "00:00") {
6+
// return `12:00 am`;
7+
// }
8+
9+
// if (hours > 12) {
10+
// return `${hours - 12}:00 pm`;
11+
// }
12+
// return `${time} am`;
13+
// }
14+
15+
// export { formatAs12HourClock };
16+
17+
// The code which run all the run cases is.
118
function formatAs12HourClock(time) {
219
const hours = Number(time.slice(0, 2));
20+
const minutes = time.slice(-3);
321

4-
if (time === "00:00") {
5-
return `12:00 am`;
22+
if (hours === 0) {
23+
return `12${minutes} am`;
24+
}
25+
if (hours === 12) {
26+
return `${time} pm`;
627
}
7-
828
if (hours > 12) {
9-
return `${hours - 12}:00 pm`;
29+
return `${String(hours - 12).padStart(2, "0")}${minutes} pm`;
1030
}
1131
return `${time} am`;
1232
}

0 commit comments

Comments
 (0)