44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+
8+ if ( hours === 0 ) {
9+ return `12${ time . slice ( 2 ) } am` ;
10+ }
11+
12+ if ( hours === 12 ) {
13+ return `${ time } pm` ;
14+ }
715 if ( hours > 12 ) {
816 return `${ hours - 12 } :00 pm` ;
917 }
@@ -14,18 +22,18 @@ const currentOutput = formatAs12HourClock("08:00");
1422const targetOutput = "08:00 am" ;
1523console . assert (
1624 currentOutput === targetOutput ,
17- `current output: ${ currentOutput } , target output: ${ targetOutput } `
25+ `current output: ${ currentOutput } , target output: ${ targetOutput } ` ,
1826) ;
1927
2028const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
2129const targetOutput2 = "11:00 pm" ;
2230console . assert (
2331 currentOutput2 === targetOutput2 ,
24- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
32+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
2533) ;
2634
27- const currentOutput3 = formatAs12HourClock ( "24 :00" ) ;
28- const targetOutput3 = "00 :00 pm " ;
35+ const currentOutput3 = formatAs12HourClock ( "00 :00" ) ;
36+ const targetOutput3 = "12 :00 am " ;
2937console . assert (
3038 currentOutput3 === targetOutput3 ,
3139 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } ` ,
@@ -37,3 +45,10 @@ console.assert(
3745 currentOutput4 === targetOutput4 ,
3846 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
3947) ;
48+
49+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
50+ const targetOutput4 = "12:00 pm" ;
51+ console . assert (
52+ currentOutput4 === targetOutput4 ,
53+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
54+ ) ;
0 commit comments