Skip to content

Commit 1450d25

Browse files
committed
verify edge cases with console.log
1 parent 5648408 commit 1450d25

1 file changed

Lines changed: 42 additions & 13 deletions

File tree

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

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,52 @@
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

55
function formatAs12HourClock(time) {
6-
const hours = Number(time.slice(0, 2));
6+
const hours = Number(time.slice(0, 2)); // captures the hours part of the time string, e.g. "08" from "08:00"
7+
const minutes = time.slice(2); // captures the full minutes part e.g. ":30", not just ":00"
8+
if (hours === 0) {
9+
return `12${minutes} am`; // midnight edge case - 00:00 should be 12:00 am, not 0:00 am
10+
}
11+
if (hours === 12) {
12+
return `12${minutes} pm`; // noon edge case - 12:00 should be 12:00 pm, not 12:00 am
13+
}
714
if (hours > 12) {
8-
return `${hours - 12}:00 pm`;
15+
return `${hours - 12}${minutes} pm`; // afternoon - subtract 12 to convert to 12-hour format
916
}
10-
return `${time} am`;
17+
return `${time} am`; // morning hours 1-11, no change needed
1118
}
1219

20+
// === means strict equality - checks that both the value AND the type match
21+
// e.g. 1 === "1" is false (number vs string), but 1 === 1 is true
22+
// console.assert checks if the condition is true - if not, it prints the error message
23+
24+
// Existing tests
1325
const currentOutput = formatAs12HourClock("08:00");
14-
const targetOutput = "08:00 am";
15-
console.assert(
16-
currentOutput === targetOutput,
17-
`current output: ${currentOutput}, target output: ${targetOutput}`
18-
);
26+
const targetOutput = "08:00 am"; // normal morning time - no change expected
27+
console.assert(currentOutput === targetOutput, `current output: ${currentOutput}, target output: ${targetOutput}`);
1928

2029
const currentOutput2 = formatAs12HourClock("23:00");
21-
const targetOutput2 = "11:00 pm";
22-
console.assert(
23-
currentOutput2 === targetOutput2,
24-
`current output: ${currentOutput2}, target output: ${targetOutput2}`
25-
);
30+
const targetOutput2 = "11:00 pm"; // normal afternoon time - 23 - 12 = 11
31+
console.assert(currentOutput2 === targetOutput2, `current output: ${currentOutput2}, target output: ${targetOutput2}`);
32+
33+
// Additional tests for edge cases
34+
const currentOutput3 = formatAs12HourClock("00:00");
35+
const targetOutput3 = "12:00 am"; // midnight - 00:00 hours should display as 12:00 am
36+
console.assert(currentOutput3 === targetOutput3, `current output: ${currentOutput3}, target output: ${targetOutput3}`);
37+
38+
const currentOutput4 = formatAs12HourClock("12:00");
39+
const targetOutput4 = "12:00 pm"; // noon - sits on the boundary between am and pm
40+
console.assert(currentOutput4 === targetOutput4, `current output: ${currentOutput4}, target output: ${targetOutput4}`);
41+
42+
const currentOutput5 = formatAs12HourClock("01:00");
43+
const targetOutput5 = "01:00 am"; // earliest normal morning hour
44+
console.assert(currentOutput5 === targetOutput5, `current output: ${currentOutput5}, target output: ${targetOutput5}`);
45+
46+
const currentOutput6 = formatAs12HourClock("13:30");
47+
const targetOutput6 = "1:30 pm"; // tests that actual minutes (:30) are preserved, not hardcoded as :00
48+
console.assert(currentOutput6 === targetOutput6, `current output: ${currentOutput6}, target output: ${targetOutput6}`);
49+
50+
console.log(formatAs12HourClock("08:00")); // "08:00 am"
51+
console.log(formatAs12HourClock("23:00")); // "11:00 pm"
52+
console.log(formatAs12HourClock("00:00")); // "12:00 am"
53+
console.log(formatAs12HourClock("12:00")); // "12:00 pm"
54+
console.log(formatAs12HourClock("13:30")); // "1:30 pm"

0 commit comments

Comments
 (0)