44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const minutes = time . slice ( 3 , 5 ) ;
78
89 if ( hours === 0 ) {
9- return `12${ time . slice ( 2 ) } am ` ;
10+ return `12: ${ minutes } AM ` ;
1011 }
11-
1212 if ( hours === 12 ) {
13- return `${ time } pm ` ;
13+ return `12: ${ minutes } PM ` ;
1414 }
1515 if ( hours > 12 ) {
16- return `${ hours - 12 } :00 pm ` ;
16+ return `${ hours - 12 } :${ minutes } PM ` ;
1717 }
18- return `${ time } am ` ;
18+ return `${ time } AM ` ;
1919}
2020
21- const currentOutput = formatAs12HourClock ( "08 :00" ) ;
22- const targetOutput = "08 :00 am " ;
21+ const currentOutput1 = formatAs12HourClock ( "00 :00" ) ;
22+ const targetOutput1 = "12 :00 AM " ;
2323console . assert (
24- currentOutput === targetOutput ,
25- `current output: ${ currentOutput } , target output: ${ targetOutput } ` ,
24+ currentOutput1 === targetOutput1 ,
25+ `current output 1 : ${ currentOutput1 } , target output: ${ targetOutput1 } ` ,
2626) ;
2727
28- const currentOutput2 = formatAs12HourClock ( "23:00 " ) ;
29- const targetOutput2 = "11:00 pm " ;
28+ const currentOutput2 = formatAs12HourClock ( "00:01 " ) ;
29+ const targetOutput2 = "12:01 AM " ;
3030console . assert (
3131 currentOutput2 === targetOutput2 ,
32- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
32+ `current output 2 : ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
3333) ;
3434
35- const currentOutput3 = formatAs12HourClock ( "00 :00" ) ;
36- const targetOutput3 = "12 :00 am " ;
35+ const currentOutput3 = formatAs12HourClock ( "08 :00" ) ;
36+ const targetOutput3 = "08 :00 AM " ;
3737console . assert (
3838 currentOutput3 === targetOutput3 ,
39- `current output: ${ currentOutput3 } , target output: ${ targetOutput3 } ` ,
39+ `current output 3 : ${ currentOutput3 } , target output: ${ targetOutput3 } ` ,
4040) ;
4141
4242const currentOutput4 = formatAs12HourClock ( "12:00" ) ;
43- const targetOutput4 = "12:00 pm " ;
43+ const targetOutput4 = "12:00 PM " ;
4444console . assert (
4545 currentOutput4 === targetOutput4 ,
46- `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
46+ `current output 4 : ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
4747) ;
4848
49- const currentOutput4 = formatAs12HourClock ( "12:00 " ) ;
50- const targetOutput4 = "12:00 pm " ;
49+ const currentOutput5 = formatAs12HourClock ( "12:01 " ) ;
50+ const targetOutput5 = "12:01 PM " ;
5151console . assert (
52- currentOutput4 === targetOutput4 ,
53- `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } ` ,
54- ) ;
52+ currentOutput5 === targetOutput5 ,
53+ `current output 5: ${ currentOutput5 } , target output: ${ targetOutput5 } ` ,
54+ ) ;
55+
56+ const currentOutput6 = formatAs12HourClock ( "13:00" ) ;
57+ const targetOutput6 = "1:00 PM" ;
58+ console . assert (
59+ currentOutput6 === targetOutput6 ,
60+ `current output 6: ${ currentOutput6 } , target output: ${ targetOutput6 } ` ,
61+ ) ;
62+
63+ const currentOutput7 = formatAs12HourClock ( "23:00" ) ;
64+ const targetOutput7 = "11:00 PM" ;
65+ console . assert (
66+ currentOutput7 === targetOutput7 ,
67+ `current output 7: ${ currentOutput7 } , target output: ${ targetOutput7 } ` ,
68+ ) ;
69+
70+ const currentOutput8 = formatAs12HourClock ( "23:59" ) ;
71+ const targetOutput8 = "11:59 PM" ;
72+ console . assert (
73+ currentOutput8 === targetOutput8 ,
74+ `current output 8: ${ currentOutput8 } , target output: ${ targetOutput8 } ` ,
75+ ) ;
76+
77+ // Edge test cases tested for:
78+
79+ // 1) 00:00 -> 12:00 AM
80+ // 2) 00:01 -> 12:01 AM
81+ // 3) 08:00 -> 08:00 am
82+ // 4) 12:00 -> 12:00 PM
83+ // 5) 12:01 -> 12:01 PM
84+ // 6) 13:00 -> 1:00 PM
85+ // 7) 23:00 -> 11:00 PM
86+ // 8) 23:59 -> 11:59 PM
0 commit comments