44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7- if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
7+ const minutes = time . slice ( 3 , 5 ) ;
8+
9+ if ( hours === 0 ) {
10+ return `12:${ minutes } am` ;
911 }
12+
13+ if ( hours === 12 ) {
14+ return `12:${ minutes } pm` ;
15+ }
16+
17+ if ( hours > 12 ) {
18+ return `${ hours - 12 } :${ minutes } pm` ;
19+ }
20+
1021 return `${ time } am` ;
1122}
1223
@@ -23,3 +34,24 @@ console.assert(
2334 currentOutput2 === targetOutput2 ,
2435 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2536) ;
37+
38+ const currentOutput3 = formatAs12HourClock ( "12:00" ) ;
39+ const targetOutput3 = "12:00 pm" ;
40+ console . assert (
41+ currentOutput3 === targetOutput3 ,
42+ `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
43+ ) ;
44+
45+ const currentOutput4 = formatAs12HourClock ( "00:00" ) ;
46+ const targetOutput4 = "12:00 am" ;
47+ console . assert (
48+ currentOutput4 === targetOutput4 ,
49+ `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
50+ ) ;
51+
52+ const currentOutput5 = formatAs12HourClock ( "13:00" ) ;
53+ const targetOutput5 = "1:00 pm" ;
54+ console . assert (
55+ currentOutput5 === targetOutput5 ,
56+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
57+ ) ;
0 commit comments