Skip to content

Commit 796b2ee

Browse files
Fix time formatting for midnight, hours with minutes and pm/am periods
1 parent ce9cf2e commit 796b2ee

1 file changed

Lines changed: 63 additions & 7 deletions

File tree

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

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,72 @@ function formatAs12HourClock(time) {
1010
return `${time} am`;
1111
}
1212

13-
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
13+
const currentOutput = formatAs12HourClock("00:00");
14+
const targetOutput = "12:00 am";
1515
console.assert(
1616
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
17+
`current output: ${currentOutput}, target output: ${targetOutput}`,
18+
"midnight should be 12:00 am"
1819
);
19-
20-
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
20+
const currentOutput2 = formatAs12HourClock("00:30");
21+
const targetOutput2 = "12:30 am";
2222
console.assert(
2323
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
24+
`current output: ${currentOutput2}, target output: ${targetOutput2}`,
25+
"00:30 should be 12:30 am"
26+
);
27+
28+
const currentOutput3 = formatAs12HourClock("01:30");
29+
const targetOutput3 = "01:30 am";
30+
console.assert(
31+
currentOutput3 === targetOutput3,
32+
`current output: ${currentOutput3}, target output: ${targetOutput3}`,
33+
"01:30 should be 01:30 am"
34+
);
35+
const currentOutput4 = formatAs12HourClock("11:59");
36+
const targetOutput4 = "11:59 am";
37+
console.assert(
38+
currentOutput4 === targetOutput4,
39+
`current output: ${currentOutput4}, target output: ${targetOutput4}`,
40+
"11:59 should be 11:59 am"
41+
);
42+
const currentOutput5 = formatAs12HourClock("12:30");
43+
const targetOutput5 = "12:30 pm";
44+
console.assert(
45+
currentOutput5 === targetOutput5,
46+
`current output: ${currentOutput5}, target output: ${targetOutput5}`,
47+
"12:30 should be 12:30 pm"
48+
);
49+
const currentOutput6 = formatAs12HourClock("15:45");
50+
const targetOutput6 = "03:45 pm";
51+
console.assert(
52+
currentOutput6 === targetOutput6,
53+
`current output: ${currentOutput6}, target output: ${targetOutput6}`,
54+
"15:45 should be 03:45 pm"
55+
);
56+
const currentOutput7 = formatAs12HourClock("23:30");
57+
const targetOutput7 = "11:30 pm";
58+
console.assert(
59+
currentOutput7 === targetOutput7,
60+
`current output: ${currentOutput7}, target output: ${targetOutput7}`,
61+
"23:30 should be 11:30 pm"
2562
);
63+
// fixing the bugs found:
64+
function formatAs12HourClock(time) {
65+
const hours24 = Number(time.slice(0, 2)); // 23:00 ---> "23"
66+
const minutes = time.slice(-2); // 23:00 ---> "00"
67+
68+
let period = "am";
69+
let hours12 = hours24;
70+
71+
if (hours24 === 0) {
72+
hours12 = 12; // midnight
73+
} else if (hours24 === 12) {
74+
period = "pm"; // noon
75+
} else if (hours24 > 12) {
76+
hours12 = hours24 - 12;
77+
period = "pm";
78+
}
79+
80+
return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`; // converts the return output to string type and padded to 2 length.
81+
}

0 commit comments

Comments
 (0)