File tree Expand file tree Collapse file tree
18 - Adding Up Times with Reduce Expand file tree Collapse file tree Original file line number Diff line number Diff line change 177177 < li data-time ="1:56 ">
178178 Video 57
179179 </ li >
180- < li data-time ="4:04 ">
180+ < li data-time ="4:01 ">
181181 Video 58
182182 </ li >
183183 </ ul >
184184< script >
185+
186+ const items = Array . from ( document . querySelectorAll ( "[data-time]" ) ) ;
187+ let totalSeconds = items
188+ . map ( item => item . dataset . time )
189+ . map ( item => {
190+ const [ m , s ] = item . split ( ":" ) . map ( parseFloat ) ;
191+ return m * 60 + s ;
192+ } )
193+ . reduce ( ( total , seconds ) => total + seconds ) ;
194+
195+ const hrs = Math . floor ( totalSeconds / 3600 ) ;
196+ totalSeconds = totalSeconds % 3600 ;
197+ const mins = Math . floor ( totalSeconds / 60 ) ;
198+ totalSeconds = totalSeconds % 60 ;
199+ const secs = totalSeconds ;
200+
201+ console . log ( `${ hrs } hrs ${ mins } mins ${ secs } secs` ) ;
202+
185203</ script >
186204</ body >
187205</ html >
You can’t perform that action at this time.
0 commit comments