11// This is the latest solution to the problem from the prep.
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.
4-
4+ ;
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const minutes = time . slice ( 3 , 5 ) ;
8+ if ( h )
9+
10+ if ( hours === 0 ) {
11+ return `12:${ minutes } am` ;
12+ }
13+
14+ if ( hours === 12 ) {
15+ return `12:${ minutes } pm` ;
16+ }
17+
718 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
19+ return `${ hours - 12 } :${ minutes } pm` ;
920 }
21+
1022 return `${ time } am` ;
1123}
1224
13- const currentOutput = formatAs12HourClock ( "08:00" ) ;
14- const targetOutput = "08:00 am" ;
25+ // Existing tests
26+ console . assert (
27+ formatAs12HourClock ( "08:00" ) === "08:00 am" ,
28+ "08:00 should be 08:00 am"
29+ ) ;
30+
31+ console . assert (
32+ formatAs12HourClock ( "23:00" ) === "11:00 pm" ,
33+ "23:00 should be 11:00 pm"
34+ ) ;
35+
36+ // Additional tests
37+
38+ // Midnight
1539console . assert (
16- currentOutput === targetOutput ,
17- `current output: ${ currentOutput } , target output: ${ targetOutput } `
40+ formatAs12HourClock ( "00:00" ) === "12:00 am" ,
41+ "00:00 should be 12:00 am"
1842) ;
1943
20- const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
21- const targetOutput2 = "11:00 pm" ;
44+ // Noon
2245console . assert (
23- currentOutput2 === targetOutput2 ,
24- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
46+ formatAs12HourClock ( "12:00" ) === "12:00 pm" ,
47+ "12:00 should be 12:00 pm"
2548) ;
49+
50+ // PM with minutes
51+ console . assert (
52+ formatAs12HourClock ( "13:30" ) === "1:30 pm" ,
53+ "13:30 should be 1:30 pm"
54+ ) ;
55+
56+ // AM with minutes
57+ console . assert (
58+ formatAs12HourClock ( "09:45" ) === "09:45 am" ,
59+ "09:45 should be 09:45 am"
60+ ) ;
61+
62+ // Last minute of the day
63+ console . assert (
64+ formatAs12HourClock ( "23:59" ) === "11:59 pm" ,
65+ "23:59 should be 11:59 pm"
66+ ) ;
67+
68+ // One minute after midnight
69+ console . assert (
70+ formatAs12HourClock ( "00:01" ) === "12:01 am" ,
71+ "00:01 should be 12:01 am"
72+ ) ;
73+
74+ // One minute after noon
75+ console . assert (
76+ formatAs12HourClock ( "12:01" ) === "12:01 pm" ,
77+ "12:01 should be 12:01 pm"
78+ ) ;
79+
80+ // 1 PM
81+ console . assert (
82+ formatAs12HourClock ( "13:00" ) === "1:00 pm" ,
83+ "13:00 should be 1:00 pm"
84+ ) ;
0 commit comments