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
55function formatAs12HourClock ( time ) {
6+ if ( ! / ^ \d { 2 } : \d { 2 } $ / . test ( time ) ) { //If input does not match HH:MM format, returns string "Invalid time format"
7+ return "Invalid time format" ;
8+ }
9+
610 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7- if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
9- }
11+ const minutes = time . slice ( 3 ) ;
12+ if ( hours === 0 ) return `12:${ minutes } am` ;
13+ if ( hours === 12 ) return `12:${ minutes } pm` ;
14+ if ( hours === 24 ) return `12:${ minutes } am` ;
15+ if ( hours > 12 ) return `${ hours - 12 } :00 pm` ;
16+
1017 return `${ time } am` ;
1118}
1219
@@ -23,3 +30,12 @@ console.assert(
2330 currentOutput2 === targetOutput2 ,
2431 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2532) ;
33+
34+ console . log ( formatAs12HourClock ( "12:00" ) ) ; //
35+ console . log ( formatAs12HourClock ( "00:00" ) ) ; //
36+ console . log ( formatAs12HourClock ( "15:30" ) ) ; //
37+ console . log ( formatAs12HourClock ( "11:45" ) ) ; //
38+ console . log ( formatAs12HourClock ( "24:00" ) ) ; //
39+ console . log ( formatAs12HourClock ( "ab:cd" ) ) ; //
40+ console . log ( formatAs12HourClock ( "9:00" ) ) ; //
41+ console . log ( formatAs12HourClock ( "09:0" ) ) ;
0 commit comments