1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
+
3
4
< head >
4
5
< meta charset ="UTF-8 ">
5
6
< title > Videos</ title >
6
7
</ head >
8
+
7
9
< body >
8
10
< ul class ="videos ">
9
11
< li data-time ="5:43 ">
181
183
Video 58
182
184
</ li >
183
185
184
- < script >
185
-
186
- const timeNodes = Array . from ( document . querySelectorAll ( '[data-time]' ) ) ;
186
+ < script >
187
+ ( ( ) => {
188
+ // Hide all the list items because I don't want to see all that nonsense
189
+ // document.querySelectorAll('[data-time]').forEach(listItem => listItem.hidden = true)
187
190
188
- const seconds = timeNodes
189
- . map ( node => node . dataset . time )
190
- . map ( timeCode => {
191
- const [ mins , secs ] = timeCode . split ( ':' ) . map ( parseFloat ) ;
192
- return ( mins * 60 ) + secs ;
193
- } )
194
- . reduce ( ( total , vidSeconds ) => total + vidSeconds ) ;
191
+ // Declare a constant and define as an ARRAY of all items with a 'data-key'
192
+ // property (used spread operator to create array)
193
+ const allTimes = [ ...document . querySelectorAll ( '[data-time]' ) ]
195
194
196
- let secondsLeft = seconds ;
197
- const hours = Math . floor ( secondsLeft / 3600 ) ;
198
- secondsLeft = secondsLeft % 3600 ;
195
+ /** Declare a constant and define as the return value of mapping over each item
196
+ * in the array TWICE and reducing the result of those maps to a single numeric
197
+ * value.
198
+ */
199
+ const seconds = allTimes
200
+ . map ( listItem => listItem . dataset . time ) // Get the 'data-time' property
201
+ . map ( timeValue => {
202
+ /** Declare constants minutes & seconds using array destructuring
203
+ * and define as the result of splitting the 'data-time' property
204
+ * a list item at ':' and parsing the returned two strings
205
+ * as float numbers
206
+ */
207
+ const [ minutes , seconds ] = timeValue . split ( ':' ) . map ( parseFloat )
208
+ return minutes * 60 + seconds // Return the total seconds
209
+ } )
210
+ // Add up all the seconds
211
+ . reduce ( ( runningTotal , seconds ) => runningTotal + seconds )
199
212
200
- const mins = Math . floor ( secondsLeft / 60 ) ;
201
- secondsLeft = secondsLeft % 60 ;
202
-
203
- console . log ( hours , mins , secondsLeft ) ;
204
-
205
- </ script >
213
+ /** Calculate hours by dividing total seconds by 3600 (seconds in a minute (60)
214
+ * multiplied by minutes in an hour (60)) and round the value DOWN before
215
+ * returning
216
+ */
217
+ const hours = Math . floor ( seconds / 3600 )
218
+ // Set variable to remainder of seconds after calculating hours
219
+ let secondsLeft = seconds % 3600
220
+ // Calculate remaining minutes
221
+ const minutes = Math . floor ( secondsLeft / 60 )
222
+ // Calculate remaining seconds
223
+ secondsLeft %= 60
224
+ // Log 'em out!
225
+ console . log ( `Total video time: ${ hours } hours, ${ minutes } minutes, and ${ secondsLeft } seconds` )
226
+ } ) ( ) ;
227
+ </ script >
206
228
</ body >
207
- </ html >
229
+
230
+ </ html >
0 commit comments