44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7- const mins = time . slice ( 3 , 5 ) ;
7+ const mins = time . slice ( - 2 ) ;
88 if ( hours > 12 ) {
9- return `${ hours - 12 } :${ mins } pm` ;
9+ return `${ ( hours - 12 ) . toString ( ) . padStart ( 2 , '0' ) } :${ mins } pm` ;
1010 }
1111 else if ( hours == 0 ) {
12- return `${ hours + 12 } :${ mins } am` ;
12+ return `12 :${ mins } am` ;
1313 }
1414 else if ( hours == 12 ) {
1515 return `${ time } pm` ;
@@ -31,23 +31,30 @@ console.assert(
3131 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
3232) ;
3333
34- const currentOutput3 = formatAs12HourClock ( "23:59" ) ; //
34+ const currentOutput3 = formatAs12HourClock ( "23:59" ) ;
3535const targetOutput3 = "11:59 pm" ;
3636console . assert (
3737 currentOutput3 === targetOutput3 ,
3838 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
3939) ;
4040
41- const currentOutput4 = formatAs12HourClock ( "00:00" ) ; //
41+ const currentOutput4 = formatAs12HourClock ( "00:00" ) ;
4242const targetOutput4 = "12:00 am" ;
4343console . assert (
4444 currentOutput4 === targetOutput4 ,
4545 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
4646) ;
4747
48- const currentOutput5 = formatAs12HourClock ( "12:00" ) ; //
48+ const currentOutput5 = formatAs12HourClock ( "12:00" ) ;
4949const targetOutput5 = "12:00 pm" ;
5050console . assert (
5151 currentOutput5 === targetOutput5 ,
5252 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
53+ ) ;
54+
55+ const currentOutput6 = formatAs12HourClock ( "13:00" ) ;
56+ const targetOutput6 = "01:00 pm" ;
57+ console . assert (
58+ currentOutput6 === targetOutput6 ,
59+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
5360) ;
0 commit comments