44
55function formatAs12HourClock ( time ) {
66 let hours = time . slice ( 0 , 2 ) ;
7+ let minutes = time . slice ( 3 , 5 ) ;
8+
79 if ( Number ( hours ) > 12 ) {
810 hours = String ( hours - 12 ) ;
911 hours = hours . padStart ( 2 , "0" ) ;
1012
11- return `${ hours } :00 pm` ;
13+ return `${ hours } :${ minutes } pm` ;
1214 }
13- return `${ time } am` ;
15+ return `${ hours } : ${ minutes } am` ;
1416}
1517
1618let currentOutput = formatAs12HourClock ( "14:00" ) ;
@@ -27,22 +29,22 @@ console.assert(
2729 `current output: ${ currentOutput } , target output: ${ targetOutput } `
2830) ;
2931//==========================
30- currentOutput = formatAs12HourClock ( "07:00 " ) ;
31- targetOutput = "07:00 am" ;
32+ currentOutput = formatAs12HourClock ( "07:51 " ) ;
33+ targetOutput = "07:51 am" ;
3234console . assert (
3335 currentOutput === targetOutput ,
3436 `current output: ${ currentOutput } , target output: ${ targetOutput } `
3537) ;
3638//==========================
37- currentOutput = formatAs12HourClock ( "00:00 " ) ;
38- targetOutput = "00:00 am" ;
39+ currentOutput = formatAs12HourClock ( "00:37 " ) ;
40+ targetOutput = "00:37 am" ;
3941console . assert (
4042 currentOutput === targetOutput ,
4143 `current output: ${ currentOutput } , target output: ${ targetOutput } `
4244) ;
4345//==========================
44- currentOutput = formatAs12HourClock ( "20:00 " ) ;
45- targetOutput = "08:00 pm" ;
46+ currentOutput = formatAs12HourClock ( "20:59 " ) ;
47+ targetOutput = "08:59 pm" ;
4648console . assert (
4749 currentOutput === targetOutput ,
4850 `current output: ${ currentOutput } , target output: ${ targetOutput } `
0 commit comments