Skip to content

Commit 80f2f80

Browse files
committed
Challenge 18 complete, readme initialized.
1 parent 813d6e2 commit 80f2f80

File tree

3 files changed

+47
-207
lines changed

3 files changed

+47
-207
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Exercise 18: Adding Up Times With Reduce
2+
Nitish Dayal, Software & Applications Developer - [Contact](http://nitishdayal.me)
3+
Last Commit Date: Dec 24, 2016
4+

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

Lines changed: 0 additions & 187 deletions
This file was deleted.

exercises/18 - Adding Up Times with Reduce/index-FINISHED.html renamed to exercises/18 - Adding Up Times with Reduce/index.html

Lines changed: 43 additions & 20 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,27 +183,48 @@
181183
Video 58
182184
</li>
183185

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)
187190

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]')]
195194

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)
199212

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>
206228
</body>
207-
</html>
229+
230+
</html>

0 commit comments

Comments
 (0)