Skip to content

Commit c72aac2

Browse files
committed
Refactor formatAs12HourClock function and add comprehensive test cases for various time formats and edge cases
1 parent 38ee172 commit c72aac2

1 file changed

Lines changed: 38 additions & 2 deletions

File tree

‎Sprint-3/5-stretch-extend/format-time.js‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function formatAs12HourClock(time){
4040

4141
return `${String(hours12).padStart(2, "0")}:${minutes} ${period}`;
4242
}
43-
console.log(formatAs12HourClock("23:20"))
43+
console.log(formatAs12HourClock("23:00"))
4444

4545
function check(input, targetOutput){
4646
const currentOutput = formatAs12HourClock(input);
@@ -56,4 +56,40 @@ function checkThrows(input){
5656
threw = true;
5757
}
5858
console.assert(threw, `input: ${String(input)} should have thrown an error`);
59-
}
59+
}
60+
61+
// original tests
62+
check("08:00", "08:00am");
63+
check("23:00", "11:00pm");
64+
65+
// midnight hours
66+
check("00:00", "12:00am");
67+
check("00:30", "12:30am");
68+
check("00:59", "12:59am");
69+
70+
// mornings
71+
check("01:00", "01:00am");
72+
check("09:59", "09:59am");
73+
check("10:05", "10:05am");
74+
check("11:59", "11:59am");
75+
76+
// noon
77+
check("12:00", "12:00pm");
78+
check("12:01", "12:01pm");
79+
check("12:30", "12:30pm");
80+
check("12:59", "12:59pm");
81+
82+
// mid-day and evenings
83+
check("13:00", "01:00pm");
84+
check("17:45", "05:45pm");
85+
check("22:10", "10:10pm");
86+
check("23:45", "11:45pm");
87+
check("23:59", "11:59pm");
88+
89+
// invalid times
90+
checkThrows("24:00", "25:00", "12:60", "99:99", undefined, null, 800, ["08:00"]);
91+
92+
// bad formatting
93+
checkThrows("", "abc", "8:00", "0800", "08:00:00", " 08:00", "08:00 am");
94+
95+
console.log("Complete! No errors.");

0 commit comments

Comments
 (0)