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+ // function formatAs12HourClock(time) {
6+ // const hours = Number(time.slice(0, 2));
7+ // if (hours > 12) {
8+ // return `${hours - 12}:00 pm`;
9+ // }
10+ // return `${time} am`;
11+ // }
12+
13+ // Updated function:
514function formatAs12HourClock ( time ) {
615 const hours = Number ( time . slice ( 0 , 2 ) ) ;
16+ const minutes = time . slice ( 3 , 5 ) ;
17+
18+ if ( hours === 0 ) {
19+ return `12:${ minutes } am` ;
20+ }
21+
22+ if ( hours === 12 ) {
23+ return `12:${ minutes } pm` ;
24+ }
25+
726 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
27+ return `${ hours - 12 } :${ minutes } pm` ;
928 }
29+
1030 return `${ time } am` ;
1131}
1232
@@ -23,3 +43,20 @@ console.assert(
2343 currentOutput2 === targetOutput2 ,
2444 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
2545) ;
46+
47+ // Morning times
48+ console . assert ( formatAs12HourClock ( "01:00" ) === "01:00 am" ) ;
49+ console . assert ( formatAs12HourClock ( "11:59" ) === "11:59 am" ) ;
50+
51+ // Noon
52+ console . assert ( formatAs12HourClock ( "12:00" ) === "12:00 pm" ) ;
53+ console . assert ( formatAs12HourClock ( "12:30" ) === "12:30 pm" ) ;
54+
55+ // Afternoon / evening
56+ console . assert ( formatAs12HourClock ( "13:00" ) === "1:00 pm" ) ;
57+ console . assert ( formatAs12HourClock ( "18:45" ) === "6:45 pm" ) ;
58+ console . assert ( formatAs12HourClock ( "23:59" ) === "11:59 pm" ) ;
59+
60+ // Midnight
61+ console . assert ( formatAs12HourClock ( "00:00" ) === "12:00 am" ) ;
62+ console . assert ( formatAs12HourClock ( "00:15" ) === "12:15 am" ) ;
0 commit comments