55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
77 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
8+ return `${ hours - 12 < 10 ? "0" : "" } ${ hours - 12 } :${ time . slice ( - 2 ) } pm` ;
9+ } else if ( hours === 12 ) {
10+ return `${ time } pm` ;
911 }
1012 return `${ time } am` ;
1113}
@@ -14,12 +16,40 @@ const currentOutput = formatAs12HourClock("08:00");
1416const targetOutput = "08:00 am" ;
1517console . assert (
1618 currentOutput === targetOutput ,
17- `current output: ${ currentOutput } , target output: ${ targetOutput } `
19+ `current output: ${ currentOutput } , target output: ${ targetOutput } ` ,
1820) ;
1921
2022const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
2123const targetOutput2 = "11:00 pm" ;
2224console . assert (
2325 currentOutput2 === targetOutput2 ,
24- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
26+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
27+ ) ;
28+
29+ const currentOutput3 = formatAs12HourClock ( "12:00" ) ;
30+ const targetOutput3 = "12:00 pm" ;
31+ console . assert (
32+ currentOutput3 === targetOutput3 ,
33+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } ` ,
34+ ) ;
35+
36+ const currentOutput4 = formatAs12HourClock ( "15:45" ) ;
37+ const targetOutput4 = "03:45 pm" ;
38+ console . assert (
39+ currentOutput4 === targetOutput4 ,
40+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
41+ ) ;
42+
43+ const currentOutput5 = formatAs12HourClock ( "08:25" ) ;
44+ const targetOutput5 = "08:25 am" ;
45+ console . assert (
46+ currentOutput5 === targetOutput5 ,
47+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } ` ,
48+ ) ;
49+
50+ const currentOutput6 = formatAs12HourClock ( "12:17" ) ;
51+ const targetOutput6 = "12:17 pm" ;
52+ console . assert (
53+ currentOutput6 === targetOutput6 ,
54+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } ` ,
2555) ;
0 commit comments