File tree Expand file tree Collapse file tree 1 file changed +19
-5
lines changed
Expand file tree Collapse file tree 1 file changed +19
-5
lines changed Original file line number Diff line number Diff line change 11function formatAs12HourClock ( time ) {
2- if ( Number ( time . slice ( 0 , 2 ) ) > 12 ) {
3- return `${ Number ( time . slice ( 0 , 2 ) ) - 12 } :00 pm` ;
2+ const hour = Number ( time . slice ( 0 , 2 ) ) ;
3+ const minute = time . slice ( 3 ) ;
4+
5+ if ( hour === 0 ) {
6+ return `12:${ minute } am` ;
7+ } else if ( hour === 12 ) {
8+ return `12:${ minute } pm` ;
9+ } else if ( hour > 12 ) {
10+ return `${ hour - 12 } :${ minute } pm` ;
11+ } else {
12+ return `${ time } am` ;
413 }
5- return `${ time } am` ;
614}
7-
815const currentOutput = formatAs12HourClock ( "08:00" ) ;
916const targetOutput = "08:00 am" ;
1017console . assert (
@@ -24,4 +31,11 @@ console.assert(
2431 currentOutput2 === targetOutput2 ,
2532 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2633) ;
27- console . log ( currentOutput3 , targetOutput3 ) ;
34+ console . log ( currentOutput3 , targetOutput3 ) ;
35+
36+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
37+ const targetOutput4 = "12:00 pm" ;
38+ console . assert (
39+ currentOutput4 === targetOutput4 ,
40+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
41+ ) ;
You can’t perform that action at this time.
0 commit comments