Skip to content

Commit 2c0d4ff

Browse files
committed
Finished wesbos#18
1 parent a77c001 commit 2c0d4ff

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

18 - Adding Up Times with Reduce/index-START.html

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,48 @@
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>

0 commit comments

Comments
 (0)