Skip to content

Commit 8913624

Browse files
committed
Add test cases for formatAs12HourClock function and document edge cases and bugs found
1 parent 5a7c70d commit 8913624

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

Sprint-2/4-mandatory-interpret/time-format.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ console.log(formatTimeDisplay(61)); // This line is inserted only to test the fu
2222
// =============> write your answer here
2323
//3 times
2424
// Call formatTimeDisplay with an input of 61, now answer the following:
25-
// 00:01:01
25+
2626
// b) What is the value assigned to num when pad is called for the first time?
2727
// =============> write your answer here
2828
//61
2929
// c) What is the return value of pad is called for the first time?
3030
// =============> write your answer here
31-
31+
//0
3232
// d) What is the value assigned to num when pad is called for the last time in this program? Explain your answer
3333
// =============> write your answer here
34-
34+
/*
35+
The value assigned to num when pad is called for the last time is 1.
36+
The last call to pad in the template literal is pad(remainingSeconds). Since remainingSeconds = 61 % 60 = 1, the value passed to num is 1. This will return "01" after padding, resulting in the final output "00:01:01"
37+
*/
3538
// e) What is the return value assigned to num when pad is called for the last time in this program? Explain your answer
3639
// =============> write your answer here
40+
/*
41+
The return value of the last call to pad is "01".
42+
Explanation: When pad is called with remainingSeconds (which equals 1), the parameter num receives the value 1. The function then converts it to a string "1",
43+
pads it to 2 characters with leading zeros to get "01", and returns this string. This returned value "01"
44+
is then inserted into the template literal as the last part of the time display, producing the final output "00:01:01".
45+
*/

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,49 @@ console.assert(
2323
currentOutput2 === targetOutput2,
2424
`current output: ${currentOutput2}, target output: ${targetOutput2}`
2525
);
26+
console.log(formatAs12HourClock("12:00")); // This line is inserted only to test the function. It returns "12:00 am"
27+
console.log(formatAs12HourClock("12:01")); // This line is inserted only to test the function. It returns "12:00 pm"
28+
console.log(formatAs12HourClock("13:13")); // This line is inserted only to test the function. It returns "12:00 am"
29+
console.log(formatAs12HourClock("00:01")); // This line is inserted only to test the function. It returns "12:00 pm"
30+
console.log(formatAs12HourClock("01:01")); // This line is inserted only to test the function. It returns "12:00 am"
31+
console.log(formatAs12HourClock("11:59")); // This line is inserted only to test the function. It returns "12:00 am"
32+
console.log(formatAs12HourClock("24:00")); // This line is inserted only to test the function. It returns "12:00 am"
33+
console.log(formatAs12HourClock("14:14")); // This line is inserted only to test the function. It returns "12:00 am"
34+
console.log(formatAs12HourClock("20:20")); // This line is inserted only to test the function. It returns "12:00 am"
35+
console.log(formatAs12HourClock("00:00")); // This line is inserted only to test the function. It returns "12:00 am"
36+
37+
/*
38+
Test Groups Created:
39+
1. Midnight (00:xx) - Edge case for start of day
40+
2. Morning hours (01-11) - Standard AM times
41+
3. Noon (12:xx) - Edge case for 12 PM
42+
4. Afternoon/Evening (13-23) - Standard PM times requiring conversion
43+
5. Minutes preservation - Ensures minutes aren't lost
44+
45+
Bugs Found:
46+
1. Midnight bug: "00:00" returns "00:00 am" instead of "12:00 am"
47+
2. Noon bug: "12:00" returns "12:00 am" instead of "12:00 pm"
48+
3. Minutes lost: All times lose their minutes and show :00 (e.g., "13:13" becomes "1:00 pm" instead of "1:13 pm")
49+
4. Leading zeros: Morning times keep the input format including leading zeros
50+
*/
51+
52+
/*
53+
function formatAs12HourClock(time) {
54+
const hours = Number(time.slice(0, 2));
55+
const minutes = time.slice(3, 5);
56+
57+
if (hours === 0) {
58+
// Midnight: 00:xx becomes 12:xx am
59+
return `12:${minutes} am`;
60+
} else if (hours < 12) {
61+
// Morning: 1-11 stays same with am
62+
return `${hours}:${minutes} am`;
63+
} else if (hours === 12) {
64+
// Noon: 12:xx stays 12:xx pm
65+
return `12:${minutes} pm`;
66+
} else {
67+
// Afternoon/Evening: 13-23 subtract 12 and add pm
68+
return `${hours - 12}:${minutes} pm`;
69+
}
70+
}
71+
*/

0 commit comments

Comments
 (0)