Skip to content

Commit f30e590

Browse files
committed
Added Adding Up Times with Reduce (wesbos#18)
1 parent a94b5a2 commit f30e590

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

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

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Videos</title>
67
</head>
8+
79
<body>
810
<ul class="videos">
911
<li data-time="5:43">
@@ -181,7 +183,26 @@
181183
Video 58
182184
</li>
183185
</ul>
184-
<script>
185-
</script>
186+
<script>
187+
function displayTime(seconds) {
188+
let secLeft = seconds;
189+
const hh = Math.floor(secLeft / 3600);
190+
secLeft = secLeft % 3600;
191+
const mm = Math.floor(secLeft / 60);
192+
ss = secLeft % 60;
193+
194+
return `${hh}:${mm}:${ss}`;
195+
}
196+
197+
const seconds = [...document.querySelectorAll('[data-time]')]
198+
.map(n => n.dataset.time)
199+
.map(t => {
200+
const [mins, secs] = t.split(':').map(parseFloat);
201+
return (mins * 60) + secs;
202+
}).reduce((total, duration) => total + duration);
203+
204+
console.log('Total time = ' + displayTime(seconds));
205+
</script>
186206
</body>
187-
</html>
207+
208+
</html>

0 commit comments

Comments
 (0)