22// Make sure to do the prep before you do the coursework
33// Your task is to write tests for as many different groups of input data or edge cases as you can, and fix any bugs you find.
44
5+ const pad = ( num ) => String ( num ) . padStart ( 2 , "0" ) ;
6+
57function formatAs12HourClock ( time ) {
68 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7- if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
9+ const minutes = time . slice ( 3 , 5 ) ;
10+
11+ if ( hours === 0 ) {
12+ return `12:${ minutes } am` ;
13+ }
14+ else if ( hours > 12 ) {
15+ return `${ pad ( hours - 12 ) } :${ minutes } pm` ;
16+ }
17+ else if ( hours === 12 ) {
18+ return `${ hours } :${ minutes } pm` ;
19+ }
20+ else {
21+ return `${ pad ( hours ) } :${ minutes } am` ;
922 }
10- return `${ time } am` ;
1123}
1224
1325const currentOutput = formatAs12HourClock ( "08:00" ) ;
@@ -25,14 +37,14 @@ console.assert(
2537) ;
2638
2739const currentOutput3 = formatAs12HourClock ( "13:15" ) ;
28- const targetOutput3 = "1 :15 pm" ;
40+ const targetOutput3 = "01 :15 pm" ;
2941console . assert (
3042 currentOutput3 === targetOutput3 ,
3143 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } `
3244) ;
3345
3446const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
35- const targetOutput4 = "12:00 am " ;
47+ const targetOutput4 = "12:00 pm " ;
3648console . assert (
3749 currentOutput4 === targetOutput4 ,
3850 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
0 commit comments