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.
44function pad ( num ) {
55 let numString = num . toString ( ) ;
6- while ( numString . length < 2 ) {
6+ if ( numString . length < 2 ) {
77 numString = "0" + numString ;
88 }
99 return numString ;
1010}
1111
1212function formatAs12HourClock ( time ) {
1313 const hours = Number ( time . slice ( 0 , 2 ) ) ;
14- const minute = pad ( Number ( time . slice ( 3 , 5 ) ) ) ;
14+ const minute = time . slice ( 3 , 5 ) ;
1515 if ( hours > 12 ) {
16- return `${ hours - 12 } :${ minute } pm` ;
17- } else if ( hours == 12 & minute == '00' ) {
18- return `${ time } pm` ;
16+ return `${ pad ( hours - 12 ) } :${ minute } pm` ;
17+ } else if ( hours == 12 ) {
18+ return `12:${ minute } pm` ;
19+ } else if ( hours == 00 ) {
20+ return `12:${ minute } am` ;
1921 }
2022 return `${ time } am` ;
2123}
@@ -49,7 +51,7 @@ console.assert(
4951) ;
5052
5153const currentOutput5 = formatAs12HourClock ( "00:00" ) ;
52- const targetOutput5 = "00 :00 am" ;
54+ const targetOutput5 = "12 :00 am" ;
5355console . assert (
5456 currentOutput5 === targetOutput5 ,
5557 `current output: ${ currentOutput5 } , target output: ${ targetOutput5 } `
@@ -60,4 +62,46 @@ const targetOutput6 = "12:00 pm";
6062console . assert (
6163 currentOutput6 === targetOutput6 ,
6264 `current output: ${ currentOutput6 } , target output: ${ targetOutput6 } `
63- ) ;
65+ ) ;
66+
67+ const currentOutput7 = formatAs12HourClock ( "15:25" ) ;
68+ const targetOutput7 = "03:25 pm" ;
69+ console . assert (
70+ currentOutput7 === targetOutput7 ,
71+ `current output: ${ currentOutput7 } , target output: ${ targetOutput7 } `
72+ ) ;
73+
74+ const currentOutput8 = formatAs12HourClock ( "00:08" ) ;
75+ const targetOutput8 = "12:08 am" ;
76+ console . assert (
77+ currentOutput8 === targetOutput8 ,
78+ `current output: ${ currentOutput8 } , target output: ${ targetOutput8 } `
79+ ) ;
80+
81+ const currentOutput9 = formatAs12HourClock ( "08:35" ) ;
82+ const targetOutput9 = "08:35 am" ;
83+ console . assert (
84+ currentOutput9 === targetOutput9 ,
85+ `current output: ${ currentOutput9 } , target output: ${ targetOutput9 } `
86+ ) ;
87+
88+ const currentOutput10 = formatAs12HourClock ( "20:35" ) ;
89+ const targetOutput10 = "08:35 pm" ;
90+ console . assert (
91+ currentOutput10 === targetOutput10 ,
92+ `current output: ${ currentOutput10 } , target output: ${ targetOutput10 } `
93+ ) ;
94+
95+ const currentOutput11 = formatAs12HourClock ( "12:34" ) ;
96+ const targetOutput11 = "12:34 pm" ;
97+ console . assert (
98+ currentOutput11 === targetOutput11 ,
99+ `current output: ${ currentOutput11 } , target output: ${ targetOutput11 } `
100+ ) ;
101+
102+ const currentOutput12 = formatAs12HourClock ( "00:34" ) ;
103+ const targetOutput12 = "12:34 am" ;
104+ console . assert (
105+ currentOutput12 === targetOutput12 ,
106+ `current output: ${ currentOutput12 } , target output: ${ targetOutput12 } `
107+ ) ;
0 commit comments