44
55function formatAs12HourClock ( time ) {
66 const hours = Number ( time . slice ( 0 , 2 ) ) ;
7+ const minutes = Number ( time . slice ( 3 , 5 ) ) ;
78 if ( hours > 12 ) {
8- return `${ hours - 12 } :00 pm` ;
9+ return `${ ( hours - 12 ) . toString ( ) . padStart ( 2 , "0" ) } :${ minutes . toString ( ) . padStart ( 2 , "0" ) } pm` ;
10+ } else if ( hours === 12 ) {
11+ return `${ hours } :${ minutes . toString ( ) . padStart ( 2 , "0" ) } pm` ;
12+ } else if ( hours === 0 ) {
13+ return `12:${ minutes . toString ( ) . padStart ( 2 , "0" ) } am` ;
914 }
1015 return `${ time } am` ;
1116}
@@ -14,12 +19,63 @@ const currentOutput = formatAs12HourClock("08:00");
1419const targetOutput = "08:00 am" ;
1520console . assert (
1621 currentOutput === targetOutput ,
17- `current output: ${ currentOutput } , target output: ${ targetOutput } `
22+ `current output: ${ currentOutput } , target output: ${ targetOutput } ` ,
1823) ;
1924
2025const currentOutput2 = formatAs12HourClock ( "23:00" ) ;
2126const targetOutput2 = "11:00 pm" ;
2227console . assert (
2328 currentOutput2 === targetOutput2 ,
24- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
29+ `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } ` ,
2530) ;
31+
32+ formatAs12HourClock ( "19:00" ) ;
33+ let output = formatAs12HourClock ( "19:00" ) ;
34+ let target = "07:00 pm" ;
35+ console . assert (
36+ output === target ,
37+ `current output: ${ output } , target output: ${ target } ` ,
38+ ) ;
39+ console . log ( formatAs12HourClock ( "19:00" ) ) ;
40+ // //works
41+ formatAs12HourClock ( "9:00" ) ;
42+ output = formatAs12HourClock ( "9:00" ) ;
43+ target = "9:00 am" ;
44+ console . assert (
45+ output === target ,
46+ `current output: ${ output } , target output: ${ target } ` ,
47+ ) ;
48+ console . log ( formatAs12HourClock ( "9:00" ) ) ;
49+ // //works
50+ formatAs12HourClock ( "12:00" ) ;
51+ output = formatAs12HourClock ( "12:00" ) ;
52+ target = "12:00 pm" ;
53+ console . assert (
54+ output === target ,
55+ `current output: ${ output } , target output: ${ target } ` ,
56+ ) ;
57+
58+ console . log ( formatAs12HourClock ( "12:00" ) ) ;
59+ //Function needs else if statement to includes code behavior when "12:00" will be the argument value.
60+
61+ console . log ( formatAs12HourClock ( "9:00" ) ) ;
62+ // //works
63+ formatAs12HourClock ( "00:00" ) ;
64+ output = formatAs12HourClock ( "00:00" ) ;
65+ target = "12:00 am" ;
66+ console . assert (
67+ output === target ,
68+ `current output: ${ output } , target output: ${ target } ` ,
69+ ) ;
70+ console . log ( formatAs12HourClock ( "00:00" ) ) ;
71+ //Function needs else if statement to includes code behavior when "00:00" will be the argument value.
72+
73+ formatAs12HourClock ( "19:37" ) ;
74+ output = formatAs12HourClock ( "19:37" ) ;
75+ target = "07:37 pm" ;
76+ console . assert (
77+ output === target ,
78+ `current output: ${ output } , target output: ${ target } ` ,
79+ ) ;
80+ console . log ( formatAs12HourClock ( "19:37" ) ) ;
81+ //For the test to pass I had to create a minutes variable to extract the minutes from the input. When minutes were converted to a number, zero at the start of the string was removed, so padStart(2, "0") was used to add it back.
0 commit comments