8585// console.log(formatAs12HourClock("23:00"))
8686
8787
88+ // Test result:
89+ // Hours and Minutes worked in the AM function for different time test.
90+ // 12:00 in the afternoon doesn't function correctly
91+ // 00:00 mid-night doesnt work
92+ // Minutes in the PM are not working.
93+
94+ // To fix bug:
95+
8896function formatAs12HourClock ( time ) {
8997 const hours = Number ( time . slice ( 0 , 2 ) ) ;
9098 const minutes = time . slice ( 3 , 5 ) ;
99+
100+ if ( hours === 0 ) {
101+ return `12:${ minutes } am`
102+ }
103+ if ( hours === 12 ) {
104+ return `12:${ minutes } pm` ;
105+ }
91106 if ( hours > 12 ) {
92- return `${ hours - 12 } :${ minute } pm` ;
107+ return `${ hours - 12 } :${ minutes } pm` ;
93108 }
94- return `${ time } am` ;
95- }
96- // console.log(formatAs12HourClock("10:15"));
109+
97110
98- // Early Morning: ================> assertion passed
99- let currentOutput = formatAs12HourClock ( "08:30" ) ;
100- let targetOutput = "08:30 am" ;
101- console . assert (
102- currentOutput === targetOutput ,
103- `current output: ${ currentOutput } , target output: ${ targetOutput } `
104- ) ;
105- console . log ( formatAs12HourClock ( "08:30" ) )
106-
107- // Mid-morning ==============> assertion passed
108- currentOutput = formatAs12HourClock ( "10:00" ) ;
109- targetOutput = "10:00 am" ;
110- console . assert (
111- currentOutput === targetOutput ,
112- `current output: ${ currentOutput } , target output: ${ targetOutput } `
113- ) ;
114- console . log ( formatAs12HourClock ( "10.00" ) )
111+ return `${ time } am` ;
112+ }
115113
116- // Late morning ============> assertion passed
117- currentOutput = formatAs12HourClock ( "11:45" ) ;
118- targetOutput = "11:45 am" ;
114+ const currentOutput = formatAs12HourClock ( "00:00" ) ;
115+ const targetOutput = "12:00 am" ;
119116console . assert (
120117 currentOutput === targetOutput ,
121118 `current output: ${ currentOutput } , target output: ${ targetOutput } `
122119) ;
123- console . log ( formatAs12HourClock ( "11.45 " ) )
120+ console . log ( formatAs12HourClock ( "00:00 " ) )
124121
122+ const currentOutput2 = formatAs12HourClock ( "12:00" ) ;
123+ const targetOutput2 = "12:00 pm" ;
125124
126- // Afternoon =============> assertion failed
127- let currentOutput2 = formatAs12HourClock ( "12:00" ) ;
128- let targetOutput2 = "12:00 pm" ;
129125console . assert (
130126 currentOutput2 === targetOutput2 ,
131127 `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
132128) ;
133- console . log ( formatAs12HourClock ( "12:00" ) )
134-
135- // Early afternoon =============> assertion passed
136- currentOutput2 = formatAs12HourClock ( "14:00" ) ;
137- targetOutput2 = "02:00 pm" ;
138- console . assert (
139- currentOutput2 === targetOutput2 ,
140- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
141- ) ;
142- console . log ( formatAs12HourClock ( "14:00" ) )
143-
144- // Mid afternoon =============> time display doesn't correspond
145- currentOutput2 = formatAs12HourClock ( "15:30" ) ;
146- targetOutput2 = "03:30 pm" ;
147- console . assert (
148- currentOutput2 === targetOutput2 ,
149- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
150- ) ;
151- console . log ( formatAs12HourClock ( "15:30" ) )
152-
153- // Towards evening ==============> time display doesn't correspond
154- currentOutput2 = formatAs12HourClock ( "17:30" ) ;
155- targetOutput2 = "05:30 pm" ;
156- console . assert (
157- currentOutput2 === targetOutput2 ,
158- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
159- ) ;
160- console . log ( formatAs12HourClock ( "17:30" ) )
161-
162- // Late night =============> assertion passed
163- currentOutput2 = formatAs12HourClock ( "23:00" ) ;
164- targetOutput2 = "11:00 pm" ;
165- console . assert (
166- currentOutput2 === targetOutput2 ,
167- `current output: ${ currentOutput2 } , target output: ${ targetOutput2 } `
168- ) ;
169- console . log ( formatAs12HourClock ( "23:00" ) )
170-
171-
172-
173-
174-
175-
176-
177-
178-
179129
130+ console . log ( formatAs12HourClock ( "12:00" ) ) ;
0 commit comments