Skip to content

Commit b00b826

Browse files
committed
5.format-time.js updated
1 parent 95da651 commit b00b826

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,48 @@ console.assert(
2323
currentOutput2 === targetOutput2,
2424
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2525
);
26+
27+
//Test cases
28+
console.assert(
29+
formatAs12HourClock("08:00") === "08:00 am",
30+
"Test 1 failed"
31+
);
32+
33+
console.assert(
34+
formatAs12HourClock("23:15") === "11:15 pm",
35+
"Test 2 failed"
36+
);
37+
38+
console.assert(
39+
formatAs12HourClock("00:05") === "12:05 am",
40+
"Test 3 failed"
41+
);
42+
43+
console.assert(
44+
formatAs12HourClock("12:30") === "12:30 pm",
45+
"Test 4 failed"
46+
);
47+
48+
console.assert(
49+
formatAs12HourClock("13:45") === "01:45 pm",
50+
"Test 5 failed"
51+
);
52+
53+
//fixed version
54+
/*function formatAs12HourClock(time) {
55+
let hours = Number(time.slice(0, 2));
56+
const minutes = time.slice(3); // keep the minutes as they are
57+
let period = "am";
58+
59+
if (hours === 0) {
60+
hours = 12; // midnight
61+
} else if (hours === 12) {
62+
period = "pm"; // noon
63+
} else if (hours > 12) {
64+
hours -= 12;
65+
period = "pm";
66+
}
67+
68+
return `${hours.toString().padStart(2, "0")}:${minutes} ${period}`;
69+
}
70+
*/

0 commit comments

Comments
 (0)