44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const minutes = time . slice ( 3 , 5 ) ;
8+
79 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
10+ const convertedHours = hours - 12 ;
11+ // Pad the hour with a leading zero if it's a single digit (e.g., 3 becomes "03")
12+ const paddedHours = String ( convertedHours ) . padStart ( 2 , "0" ) ;
13+ return `${ paddedHours } :${ minutes } pm` ;
914 }
15+
1016 return `${ time } am` ;
1117}
1218
19+ // === Your Tests (All will now pass silently!) ===
20+
1321const currentOutput = formatAs12HourClock ( "08:00" ) ;
1422const targetOutput = "08:00 am" ;
1523console . assert (
@@ -23,3 +31,11 @@ console.assert(
2331 currentOutput2 === targetOutput2 ,
2432 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2533) ;
34+
35+ console . assert ( formatAs12HourClock ( "15:00" ) === "03:00 pm" , `Failed Case D: 03:00 pm` ) ;
36+ console . assert ( formatAs12HourClock ( "13:00" ) === "01:00 pm" , `Failed Case E: 01:00 pm` ) ;
37+ console . assert ( formatAs12HourClock ( "15:45" ) === "03:45 pm" , `Failed Case D: 03:45 pm` ) ;
38+ console . assert ( formatAs12HourClock ( "13:45" ) === "01:45 pm" , `Failed Case E: 01:45 pm` ) ;
39+ console . assert ( formatAs12HourClock ( "08:45" ) === "08:45 am" , `Failed Case F: 08:45 am` ) ;
40+
41+ console . log ( "All current assertions completed!" ) ;
0 commit comments