Skip to content

Commit 521e641

Browse files
committed
Add handling and test for 12:00 (noon) case in formatAs12HourClock
and update the function to calculate the minute
1 parent 8d0bb83 commit 521e641

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

Sprint-2/Prep/12hours.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
function formatAs12HourClock(time) {
2-
if (Number(time.slice(0, 2)) > 12) {
3-
return `${Number(time.slice(0, 2)) - 12}:00 pm`;
2+
const hour = Number(time.slice(0, 2));
3+
const minute = time.slice(3);
4+
5+
if (hour === 0) {
6+
return `12:${minute} am`;
7+
} else if (hour === 12) {
8+
return `12:${minute} pm`;
9+
} else if (hour > 12) {
10+
return `${hour - 12}:${minute} pm`;
11+
} else {
12+
return `${time} am`;
413
}
5-
return `${time} am`;
614
}
7-
815
const currentOutput = formatAs12HourClock("08:00");
916
const targetOutput = "08:00 am";
1017
console.assert(
@@ -24,4 +31,11 @@ console.assert(
2431
currentOutput2 === targetOutput2,
2532
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2633
);
27-
console.log(currentOutput3,targetOutput3);
34+
console.log(currentOutput3,targetOutput3);
35+
36+
const currentOutput4 = formatAs12HourClock("12:00");
37+
const targetOutput4 = "12:00 pm";
38+
console.assert(
39+
currentOutput4 === targetOutput4,
40+
`current output: ${currentOutput4}, target output: ${targetOutput4}`
41+
);

0 commit comments

Comments
 (0)