@@ -23,3 +23,58 @@ console.assert(
2323 currentOutput2 === targetOutput2 ,
2424 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2525) ;
26+
27+ const currentOutput3 = formatAs12HourClock ( "00:00" ) ;
28+ const targetOutput3 = "12:00 am" ;
29+ console . assert (
30+ currentOutput3 === targetOutput3 ,
31+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
32+ ) ;
33+
34+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
35+ const targetOutput4 = "12:00 pm" ;
36+ console . assert (
37+ currentOutput4 === targetOutput4 ,
38+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
39+ ) ;
40+
41+ const currentOutput5 = formatAs12HourClock ( "13:00" ) ;
42+ const targetOutput5 = "1:00 pm" ;
43+ console . assert (
44+ currentOutput5 === targetOutput5 ,
45+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
46+ ) ;
47+
48+ const currentOutput7 = formatAs12HourClock ( "25:00" ) ;
49+ const targetOutput7 = "01:00 am" ;
50+ console . assert (
51+ currentOutput7 === targetOutput7 ,
52+ `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } `
53+ ) ;
54+
55+ // modified code:
56+
57+ function formatAs12HourClock ( time ) {
58+ let hours = Number ( time . slice ( 0 , 2 ) ) ;
59+ const minutes = time . slice ( 3 ) ;
60+
61+ let suffix ;
62+ if ( hours >= 12 ) {
63+ suffix = "pm" ;
64+
65+ } else {
66+ suffix = "am" ;
67+ }
68+ hours = hours % 12 || 12 ; // Convert 0 to 12, 13 to 1
69+ const formattedHours = hours . toString ( ) . padStart ( 2 , '0' ) ;
70+
71+ return `${ formattedHours } :${ minutes } ${ suffix } ` ;
72+ }
73+
74+ const currentOutput6 = formatAs12HourClock ( "23:00" ) ;
75+ const targetOutput6 = "11:00 pm" ;
76+ console . assert (
77+ currentOutput6 === targetOutput6 ,
78+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
79+ ) ;
80+
0 commit comments