Skip to content

Commit 0ee3639

Browse files
committed
Pre-video attempt at 18 Adding Up Times w/ Reduce
1 parent 0ee607c commit 0ee3639

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,33 @@
180180
<li data-time="4:04">
181181
Video 58
182182
</li>
183-
183+
</ul>
184184
<script>
185+
186+
const videoElements = document.querySelector('.videos');
187+
const videos = Array.from(videoElements.children);
188+
189+
function timeStringToSeconds(timeString) {
190+
// "5:43" -> 5 * 60 + 43
191+
const [minutes, seconds] = timeString.split(':');
192+
return Number(minutes) * 60 + Number(seconds);
193+
}
194+
195+
function secondsToTimeString(seconds) {
196+
// 343 -> "5:43"
197+
const remainingSeconds = seconds % 60;
198+
const minutes = (seconds - remainingSeconds) / 60;
199+
return `${minutes}:${remainingSeconds}`
200+
}
201+
202+
const totalSeconds = videos.reduce((accumulator, currentValue, currentIndex, array) => {
203+
return accumulator + timeStringToSeconds(currentValue.dataset.time);
204+
}, 0);
205+
const totalTimeString = secondsToTimeString(totalSeconds);
206+
console.log(totalSeconds, totalTimeString);
207+
185208
</script>
186209
</body>
187210
</html>
211+
212+

0 commit comments

Comments
 (0)