Skip to content

Commit 886d971

Browse files
committed
Test of function at different scenarios
1 parent 2b746b1 commit 886d971

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Make sure to do the prep before you do the coursework
33
// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44

5+
/**
56
function formatAs12HourClock(time) {
67
const hours = Number(time.slice(0, 2));
78
if (hours > 12) {
@@ -23,3 +24,104 @@ console.assert(
2324
currentOutput2 === targetOutput2,
2425
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2526
);
27+
*/
28+
29+
/**
30+
* function formatAs12HourClock(time) {
31+
const hours = Number(time.slice(0, 2));
32+
if (hours > 12) {
33+
return `${hours - 12}:00 pm`;
34+
}
35+
return `${time} am`;
36+
}
37+
38+
Issues
39+
40+
Minutes are hard-coded in the if branch (:00 pm) instead of using the actual minutes from the input.
41+
42+
Edge case for 12 PM: 12:00 should be 12:00 pm, but currently it returns 12:00 am.
43+
44+
Edge case for 00:00 (midnight): 00:30 should be 12:30 am, but currently it returns 00:30 am.
45+
46+
Inputs with minutes other than 00 will fail: "14:45" becomes "2:00 pm" instead of "2:45 pm".
47+
48+
Fix the function:
49+
*/
50+
51+
function formatAs12HourClock(time) {
52+
let hours = Number(time.slice(0, 2));
53+
const minutes = time.slice(3, 5);
54+
let period = "am";
55+
56+
if (hours === 0) {
57+
hours = 12; // Midnight
58+
} else if (hours === 12) {
59+
period = "pm"; // Noon
60+
} else if (hours > 12) {
61+
hours -= 12;
62+
period = "pm";
63+
}
64+
65+
return `${hours}:${minutes} ${period}`;
66+
}
67+
68+
// Test Cases:
69+
70+
console.assert(
71+
formatAs12HourClock("00:00") === "12:00 am",
72+
"Test 1 failed"
73+
);
74+
75+
console.assert(
76+
formatAs12HourClock("00:30") === "12:30 am",
77+
"Test 2 failed"
78+
);
79+
80+
console.assert(
81+
formatAs12HourClock("08:00") === "8:00 am",
82+
"Test 3 failed"
83+
);
84+
85+
console.assert(
86+
formatAs12HourClock("12:00") === "12:00 pm",
87+
"Test 4 failed"
88+
);
89+
90+
console.assert(
91+
formatAs12HourClock("12:45") === "12:45 pm",
92+
"Test 5 failed"
93+
);
94+
95+
console.assert(
96+
formatAs12HourClock("14:30") === "2:30 pm",
97+
"Test 6 failed"
98+
);
99+
100+
console.assert(
101+
formatAs12HourClock("23:59") === "11:59 pm",
102+
"Test 7 failed"
103+
);
104+
105+
console.assert(
106+
formatAs12HourClock("11:15") === "11:15 am",
107+
"Test 8 failed"
108+
);
109+
110+
console.log("All tests passed!");
111+
112+
/**
113+
Explanation of Fixes:
114+
115+
Extract the minutes separately instead of hardcoding :00.
116+
117+
Handle midnight (00:xx) as 12:xx am.
118+
119+
Handle noon (12:xx) as 12:xx pm.
120+
121+
Convert hours > 12 to 12-hour format and set period to "pm".
122+
123+
Add tests for all edge cases: midnight, noon, times with non-zero minutes, morning and evening.
124+
125+
*/
126+
127+

0 commit comments

Comments
 (0)