Skip to content

Commit 3d03d05

Browse files
committed
first revision of the function - tried to include all conditions.
1 parent d1cb728 commit 3d03d05

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
function formatAs12HourClock(time) {
2-
32
const hours = Number(time.slice(0, 2));
3+
const minutes = time.slice(-2);
44

5-
if (hours > 12) {
6-
return `${hours - 12}:00 pm`;
5+
if (hours > 24 || hours < 0) {
6+
return "invalid input.";
7+
} else if (hours === 24 || hours === 0) {
8+
return `${12}:${minutes}am`;
9+
} else if (hours === 12) {
10+
return `${hours}:${minutes}pm`;
11+
} else if (hours > 12) {
12+
return `${hours - 12}:${minutes}pm`;
13+
} else {
14+
return `${hours}:${minutes}am`;
15+
}
716
}
8-
return `${time} am`;
9-
}
1017

11-
export {formatAs12HourClock};
18+
export { formatAs12HourClock };

0 commit comments

Comments
 (0)