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+
513function formatAs12HourClock ( time ) {
614 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7- if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
15+ const minutes = time . slice ( 3 , 5 ) ;
16+
17+ if ( hours === 0 ) {
18+ return `12:${ minutes } am` ;
19+ } else if ( hours > 12 ) {
20+ return `${ hours - 12 } :${ minutes } pm` ;
21+ } else if ( hours === 12 ) {
22+ return `12:${ minutes } pm` ;
23+ } else {
24+ return `${ time } am` ;
925 }
10- return `${ time } am` ;
1126}
1227
1328//current = what the code did
@@ -33,41 +48,42 @@ console.assert(
3348 currentOutput3 === targetOutput3 ,
3449 `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } ` ,
3550) ;
36- const currentOutput4 = formatAs12HourClock ( "23:00 " ) ;
51+ const currentOutput4 = formatAs12HourClock ( "23:59 " ) ;
3752const targetOutput4 = "11:59 pm" ;
3853console . assert (
3954 currentOutput4 === targetOutput4 ,
4055 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
4156) ;
4257
4358const currentOutput5 = formatAs12HourClock ( "00:00" ) ;
44- const targetOutput5 = "00 :00 am" ;
59+ const targetOutput5 = "12 :00 am" ;
4560console . assert (
4661 currentOutput5 === targetOutput5 ,
4762 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } ` ,
4863) ;
4964
5065const currentOutput6 = formatAs12HourClock ( "00:30" ) ;
51- const targetOutput6 = "00 :30 am" ;
66+ const targetOutput6 = "12 :30 am" ;
5267console . assert (
5368 currentOutput6 === targetOutput6 ,
5469 `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } ` ,
5570) ;
5671
57- const currentOutput7 = formatAs12HourClock ( "14:00 " ) ;
58- const targetOutput7 = "14 :30 pm" ;
72+ const currentOutput7 = formatAs12HourClock ( "14:30 " ) ;
73+ const targetOutput7 = "2 :30 pm" ;
5974console . assert (
6075 currentOutput7 === targetOutput7 ,
6176 `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } ` ,
6277) ;
6378
64- const currentOutput7 = formatAs12HourClock ( "15:00 " ) ;
65- const targetOutput7 = "15 :30 pm" ;
79+ const currentOutput8 = formatAs12HourClock ( "15:30 " ) ;
80+ const targetOutput8 = "3 :30 pm" ;
6681console . assert (
67- currentOutput7 === targetOutput7 ,
68- `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } ` ,
82+ currentOutput8 === targetOutput8 ,
83+ `current output: ${ currentOutput8 } , target output: ${ targetOutput8 } ` ,
6984) ;
70- console . log ( ) ;
85+ console . log ( formatAs12HourClock ( "14:30" ) ) ;
86+ console . log ( formatAs12HourClock ( "15:30" ) ) ;
7187//Edge cases noticed:
7288// minutes are ignored in the formatAs12HourClock function after 12 so all pm hours are printed incorrectly because minutes hasn't been stored anywhere in the function.
7389// 12 should return 12:00pm but it returns 12:00am in the function
0 commit comments