File tree Expand file tree Collapse file tree
Sprint-2/5-stretch-extend Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -38,9 +38,36 @@ console.assert(
3838 `current output: ${ currentOutput4 } , target output: ${ targetOutput4 } `
3939) ;
4040
41+ const currentOutput5 = formatAs12HourClock ( "14:30" ) ;
42+ const targetOutput5 = "2:30 pm" ;
43+ console . assert (
44+ currentOutput5 === targetOutput5 ,
45+ `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
46+ ) ;
47+
4148console . log ( formatAs12HourClock ( "08:00" ) ) ;
4249console . log ( formatAs12HourClock ( "23:00" ) ) ;
4350console . log ( formatAs12HourClock ( "00:00" ) ) ;
4451console . log ( formatAs12HourClock ( "12:00" ) ) ;
52+ console . log ( formatAs12HourClock ( "14:30" ) ) ;
4553
4654//The function did not handle the edge cases 00:00 and 12:00.
55+ // The correct function should be like this
56+ function formatAs12HourClock ( time ) {
57+ const hours = Number ( time . slice ( 0 , 2 ) ) ;
58+ const minutes = time . slice ( 2 ) ;
59+
60+ if ( hours === 0 ) {
61+ return `12${ minutes } am` ;
62+ }
63+
64+ if ( hours === 12 ) {
65+ return `12${ minutes } pm` ;
66+ }
67+
68+ if ( hours > 12 ) {
69+ return `${ hours - 12 } ${ minutes } pm` ;
70+ }
71+
72+ return `${ time } am` ;
73+ }
You can’t perform that action at this time.
0 commit comments