55function formatAs12HourClock ( time ) {
66 let hours = time . slice ( 0 , 2 ) ;
77 let minutes = time . slice ( 3 , 5 ) ;
8+ let timeIndicator ;
89
9- if ( Number ( hours ) > 12 ) {
10+ if ( Number ( hours ) >= 12 ) {
1011 hours = String ( hours - 12 ) ;
1112 hours = hours . padStart ( 2 , "0" ) ;
12-
13- return `${ hours } :${ minutes } pm` ;
13+ timeIndicator = "pm" ;
14+ } else {
15+ timeIndicator = "am" ;
1416 }
15- return `${ hours } :${ minutes } am` ;
17+ if ( hours === "00" )
18+ hours = "12" ;
19+
20+ return `${ hours } :${ minutes } ${ timeIndicator } ` ;
1621}
1722
1823let currentOutput = formatAs12HourClock ( "14:00" ) ;
@@ -37,7 +42,14 @@ console.assert(
3742) ;
3843//==========================
3944currentOutput = formatAs12HourClock ( "00:37" ) ;
40- targetOutput = "00:37 am" ;
45+ targetOutput = "12:37 am" ;
46+ console . assert (
47+ currentOutput === targetOutput ,
48+ `current output: ${ currentOutput } , target output: ${ targetOutput } `
49+ ) ;
50+ //==========================
51+ currentOutput = formatAs12HourClock ( "12:37" ) ;
52+ targetOutput = "12:37 pm" ;
4153console . assert (
4254 currentOutput === targetOutput ,
4355 `current output: ${ currentOutput } , target output: ${ targetOutput } `
0 commit comments