@@ -12,45 +12,26 @@ function formatAs12HourClock(time) {
1212 suffix = "pm" ;
1313 }
1414
15- let formattedHours = String ( hours % 12 || 12 ) ;
16- formattedHours = formattedHours . padStart ( 2 , "0" ) ;
15+ const formattedHours = String ( hours % 12 || 12 ) . padStart ( 2 , "0" ) ;
1716
1817 return `${ formattedHours } :${ minutes } ${ suffix } ` ;
1918}
2019
21- const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
22- const targetOutput2 = "11:00 pm" ;
23- console . assert (
24- currentOutput2 === targetOutput2 ,
25- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
26- ) ;
27-
28- const noonOutput = formatAs12HourClock ( "12:00" ) ;
29- const noonTarget = "12:00 pm" ;
30- console . assert (
31- noonOutput === noonTarget ,
32- `current output: ${ noonOutput } , target output: ${ noonTarget } ` ,
33- ) ;
34-
35- const midnightOutput = formatAs12HourClock ( "00:00" ) ;
36- const midnightTarget = "12:00 am" ;
37- console . assert (
38- midnightOutput === midnightTarget ,
39- `current output: ${ midnightOutput } , target output: ${ midnightTarget } ` ,
40- ) ;
41-
42- const paddedHourOutput = formatAs12HourClock ( "08:00" ) ;
43- const paddedHourTarget = "08:00 am" ;
44- console . assert (
45- paddedHourOutput === paddedHourTarget ,
46- `current output: ${ paddedHourOutput } , target output: ${ paddedHourTarget } ` ,
47- ) ;
48-
49- const singleDigitMinuteOutput = formatAs12HourClock ( "9:5" ) ;
50- const singleDigitMinuteTarget = "09:05 am" ;
51- console . assert (
52- singleDigitMinuteOutput === singleDigitMinuteTarget ,
53- `current output: ${ singleDigitMinuteOutput } , target output: ${ singleDigitMinuteTarget } ` ,
54- ) ;
55-
56- console . log ( "All formatAs12HourClock tests passed." ) ;
20+ function check ( input , expected ) {
21+ const actual = formatAs12HourClock ( input ) ;
22+
23+ if ( actual !== expected ) {
24+ console . error ( `FAIL: ${ input } -> ${ actual } (expected ${ expected } )` ) ;
25+ process . exitCode = 1 ;
26+ }
27+ }
28+
29+ check ( "23:00" , "11:00 pm" ) ;
30+ check ( "12:00" , "12:00 pm" ) ;
31+ check ( "00:00" , "12:00 am" ) ;
32+ check ( "08:00" , "08:00 am" ) ;
33+ check ( "9:5" , "09:05 am" ) ;
34+
35+ if ( ! process . exitCode ) {
36+ console . log ( "All formatAs12HourClock tests passed." ) ;
37+ }
0 commit comments