@@ -6,10 +6,16 @@ function formatAs12HourClock(time) {
66 const hours = time . slice ( 0 , 2 ) ;
77 const minutes = time . slice ( 3 , 5 ) ;
88
9- if ( hours > 12 ) {
10- return `${ Number ( hours ) - 12 } :${ minutes } pm` ;
9+ if ( Number ( hours ) > 12 ) {
10+ let timeHour = Number ( hours ) - 12 ;
11+ if ( timeHour . toString ( ) . length !== 2 ) {
12+ timeHour = timeHour . toString ( ) . padStart ( 2 , "0" ) ;
13+ }
14+ return `${ timeHour } :${ minutes } pm` ;
1115 } else if ( Number ( hours ) === 0 ) {
1216 return `12:${ minutes } am` ;
17+ } else if ( Number ( hours ) === 12 ) {
18+ return `12:${ minutes } pm` ;
1319 }
1420 return `${ hours } :${ minutes } am` ;
1521}
@@ -37,15 +43,50 @@ console.assert(
3743) ;
3844
3945const currentOutput5 = formatAs12HourClock ( "15:01" ) ;
40- const targetOutput5 = "3 :01 pm" ;
46+ const targetOutput5 = "03 :01 pm" ;
4147console . assert (
4248 currentOutput5 === targetOutput5 ,
4349 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
4450) ;
4551
46- const currentOutput6 = formatAs12HourClock ( "15:01 " ) ;
47- const targetOutput6 = "3:01 pm" ;
52+ const currentOutput6 = formatAs12HourClock ( "13:00 " ) ;
53+ const targetOutput6 = "01:00 pm" ;
4854console . assert (
4955 currentOutput6 === targetOutput6 ,
5056 `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
5157) ;
58+
59+ const currentOutput7 = formatAs12HourClock ( "12:00" ) ;
60+ const targetOutput7 = "12:00 pm" ;
61+ console . assert (
62+ currentOutput7 === targetOutput7 ,
63+ `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } `
64+ ) ;
65+
66+ const currentOutput8 = formatAs12HourClock ( "12:30" ) ;
67+ const targetOutput8 = "12:30 pm" ;
68+ console . assert (
69+ currentOutput8 === targetOutput8 ,
70+ `current output: ${ currentOutput8 } , target output: ${ targetOutput8 } `
71+ ) ;
72+
73+ const currentOutput9 = formatAs12HourClock ( "11:59" ) ;
74+ const targetOutput9 = "11:59 am" ;
75+ console . assert (
76+ currentOutput9 === targetOutput9 ,
77+ `current output: ${ currentOutput9 } , target output: ${ targetOutput9 } `
78+ ) ;
79+
80+ const currentOutput10 = formatAs12HourClock ( "12:01" ) ;
81+ const targetOutput10 = "12:01 pm" ;
82+ console . assert (
83+ currentOutput10 === targetOutput10 ,
84+ `current output: ${ currentOutput10 } , target output: ${ targetOutput10 } `
85+ ) ;
86+
87+ const currentOutput11 = formatAs12HourClock ( "00:45" ) ;
88+ const targetOutput11 = "12:45 am" ;
89+ console . assert (
90+ currentOutput11 === targetOutput11 ,
91+ `current output: ${ currentOutput11 } , target output: ${ targetOutput11 } `
92+ ) ;
0 commit comments