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:06 ">
181181 Video 58
182182 </ li >
183183 </ ul >
184+
184185< script >
186+ const timeNodes = document . querySelectorAll ( "[data-time]" ) ;
187+ // console.log(timeNodes);
188+
189+ // convert node list to array
190+ const timeArray = Array . from ( timeNodes ) ;
191+ // console.log(timeArray);
192+
193+ const seconds = timeArray
194+ . map ( node => node . dataset . time )
195+ . map ( timeCode => {
196+ // use destructuring & fix returned strings
197+ const [ mins , secs ] = timeCode . split ( ':' ) . map ( parseFloat ) ;
198+
199+ return ( mins * 60 ) + secs ;
200+ } )
201+ // .reduce((total, seconds) => {
202+ // return total + seconds
203+ // })
204+
205+ // above can be simplified since
206+ // we're only returning values
207+ . reduce ( ( total , vidSecs ) => total + vidSecs ) ;
208+
209+ let secondsLeft = seconds ;
210+ const hours = Math . floor ( ( secondsLeft / 3600 ) ) ;
211+
212+ // get the leftover after dividing by 3600
213+ secondsLeft = secondsLeft % 3600 ;
214+
215+ const mins = Math . floor ( secondsLeft / 60 ) ;
216+ secondsLeft = secondsLeft % 60 ;
217+
218+ console . log ( hours , mins , secondsLeft ) ;
219+
220+ // now we have hours and seconds
185221</ script >
222+
186223</ body >
187224</ html >
You can’t perform that action at this time.
0 commit comments