44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const minutes = time . slice ( 3 , 6 ) ;
8+
79 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
10+
11+ const convertedHour = hours - 12
12+ const paddedHour = convertedHour . toString ( ) . padStart ( 2 , '0' )
13+ return `${ paddedHour } :${ minutes } pm` ;
914 }
10- return `${ time } am` ;
11- }
15+
16+ else if ( hours === 0o0 ) {
17+ const newHour = hours + 12 ;
18+ return `${ newHour } :${ minutes } am` ; }
19+
20+ else if ( hours === 12 ) {
21+ return `12:00 pm` ;
22+
23+ }
24+
25+
26+
27+ else if ( hours < 12 ) {
28+ const paddedHour2 = hours . toString ( ) . padStart ( 2 , '0' )
29+
30+ return `${ paddedHour2 } :${ minutes } am` ;
31+ }
32+ }
33+
34+
35+
36+
1237
1338const currentOutput = formatAs12HourClock ( "08:00" ) ;
1439const targetOutput = "08:00 am" ;
@@ -23,3 +48,18 @@ console.assert(
2348 currentOutput2 === targetOutput2 ,
2449 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2550) ;
51+
52+ const currentOutput3 = formatAs12HourClock ( "00:00" ) ;
53+ const targetOutput3 = "12:00 am" ;
54+ console . assert (
55+ currentOutput3 === targetOutput3 ,
56+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
57+ ) ;
58+
59+
60+ const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
61+ const targetOutput4 = "12:00 pm" ;
62+ console . assert (
63+ currentOutput4 === targetOutput4 ,
64+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
65+ ) ;
0 commit comments