44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ if ( hours === 0 || hours === 24 ) {
8+ return '12:00 am' ;
9+ }
10+ if ( hours === 12 ) {
11+ return `${ hours } :00 pm` ;
12+ }
713 if ( hours > 12 ) {
814 return `${ hours - 12 } :00 pm` ;
915 }
@@ -24,30 +30,22 @@ console.assert(
2430 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2531) ;
2632const currentOutput3 = formatAs12HourClock ( "00:00" ) ;
27- const targetOutput3 = "00 :00 am" ;
33+ const targetOutput3 = "12 :00 am" ;
2834console . assert (
2935 currentOutput3 === targetOutput3 ,
3036 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
3137) ;
3238
3339const currentOutput4 = formatAs12HourClock ( "24:00" ) ;
34- const targetOutput4 = "12:00 pm " ;
40+ const targetOutput4 = "12:00 am " ;
3541console . assert (
3642 currentOutput4 === targetOutput4 ,
3743 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
3844) ;
3945
4046const currentOutput5 = formatAs12HourClock ( "12:00" ) ;
41- const targetOutput5 = "12:00 am " ;
47+ const targetOutput5 = "12:00 pm " ;
4248console . assert (
4349 currentOutput5 === targetOutput5 ,
44- `current output: ${ currentOutput5 } , target output: ${ targetOutput } `
50+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
4551) ;
46-
47- const currentOutput5 = formatAs12HourClock ( ":00" ) ;
48- const targetOutput5 = "12:00 am" ;
49- console . assert (
50- currentOutput5 === targetOutput5 ,
51- `current output: ${ currentOutput5 } , target output: ${ targetOutput } `
52- ) ;
53-
0 commit comments