44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const minutes = time . slice ( - 2 ) ;
8+ if ( hours === 0 ) {
9+ return `12:${ minutes } am` ;
10+ }
11+ if ( hours === 12 ) {
12+ return `12:${ minutes } pm` ;
13+ }
714 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
15+ return `${ hours - 12 } :${ minutes } pm` ;
916 }
1017 return `${ time } am` ;
1118}
@@ -23,3 +30,31 @@ console.assert(
2330 currentOutput2 === targetOutput2 ,
2431 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2532) ;
33+
34+ const currentOutput3 = formatAs12HourClock ( "23:59" ) ;
35+ const targetOutput3 = "11:59 pm" ;
36+ console . assert (
37+ currentOutput3 === targetOutput3 ,
38+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
39+ ) ;
40+
41+ const currentOutput4 = formatAs12HourClock ( "00:00" ) ;
42+ const targetOutput4 = "12:00 am" ;
43+ console . assert (
44+ currentOutput4 === targetOutput4 ,
45+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
46+ ) ;
47+
48+ const currentOutput5 = formatAs12HourClock ( "12:00" ) ;
49+ const targetOutput5 = "12:00 pm" ;
50+ console . assert (
51+ currentOutput5 === targetOutput5 ,
52+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
53+ ) ;
54+
55+ const currentOutput6 = formatAs12HourClock ( "12:45" ) ;
56+ const targetOutput6 = "12:45 pm" ;
57+ console . assert (
58+ currentOutput6 === targetOutput6 ,
59+ `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
60+ ) ;
0 commit comments