File tree Expand file tree Collapse file tree 1 file changed +38
-1
lines changed
18 - Adding Up Times with Reduce Expand file tree Collapse file tree 1 file changed +38
-1
lines changed Original file line number Diff line number Diff line change 177
177
< li data-time ="1:56 ">
178
178
Video 57
179
179
</ li >
180
- < li data-time ="4:04 ">
180
+ < li data-time ="4:06 ">
181
181
Video 58
182
182
</ li >
183
183
</ ul >
184
+
184
185
< 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
185
221
</ script >
222
+
186
223
</ body >
187
224
</ html >
You can’t perform that action at this time.
0 commit comments