Skip to content

Commit d8e4f7f

Browse files
authored
Fix 12 PM formatting in time conversion function
Handle special case for 12 PM in formatAs12HourClock function.
1 parent 5ae68a9 commit d8e4f7f

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ console.assert(
2828
// noon, different minutes.
2929
function formatAs12HourClock(time) {
3030
const hours = Number(time.slice(0, 2));
31+
/*
32+
Since we know what the bug is, i shall handle it as a special case, and add the following code.
33+
34+
if (hours === 12) {
35+
return `${time} pm`;
36+
}
37+
38+
Tested it again and it works now.
39+
*/
3140
if (hours > 12) {
3241
return `${hours - 12}:00 pm`;
3342
}
@@ -42,8 +51,15 @@ console.log(formatAs12HourClock("12:00"));
4251

4352
//i have run this code in dev tools , the asnwer was 12:00am , bug found.
4453

54+
55+
4556
function formatAs12HourClock(time) {
4657
const hours = Number(time.slice(0, 2));
58+
59+
if (hours === 12) {
60+
return `${time} pm`;
61+
}
62+
4763
if (hours > 12) {
4864
return `${hours - 12}:00 pm`;
4965
}

0 commit comments

Comments
 (0)