Visitar URL original
Completed Lesson #18 · vmvictorvm/JavaScript30@80ef9a4 · GitHub
Skip to content

Commit 80ef9a4

Browse files
committed
Completed Lesson wesbos#18
1 parent 7df8cc5 commit 80ef9a4

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,39 @@
182182
</li>
183183
</ul>
184184
<script>
185+
186+
//querySelectorAll to retrieve a nodeList of <li data-time>
187+
//Use Array.from to convert nodelist into array
188+
const timeNodes = Array.from(document.querySelectorAll('[data-time]'));
189+
190+
//To get the total # of seconds for all videos
191+
//1. Use .map() to get each node.dataset.time
192+
//2. use .map() again to convert each node.dataset.time from M:S format to seconds
193+
//3. Finally use .reduce() to sum up total # of seconds for all videos.
194+
const seconds = timeNodes.map((node) => {
195+
return node.dataset.time;
196+
}).map((timeCode) => {
197+
timeCodeArray = timeCode.split(':').map(parseFloat); //From 6:10 to [6,10] //Need to use .map(parseFloat) to convert string to float so we can perform math operations
198+
totalSec = (timeCodeArray[0] * 60) + timeCodeArray[1];
199+
return totalSec;
200+
}).reduce((total,sec) => { //Use reduce to get the total of seconds
201+
total = total + sec;
202+
return total;
203+
});
204+
console.log("Total seconds: " + seconds); //Now we got the total seconds
205+
206+
//Convert total seconds into Hours:Minutes:seconds format
207+
let secondsLeft = seconds;
208+
//Get total hours
209+
const hours = Math.floor(secondsLeft / 3600);
210+
secondsLeft = secondsLeft % 3600;
211+
212+
//Get total mins
213+
const mins = Math.floor(secondsLeft / 60);
214+
secondsLeft = secondsLeft % 60;
215+
216+
217+
console.log(`${hours}:${mins}:${secondsLeft}`);
185218
</script>
186219
</body>
187220
</html>

0 commit comments

Comments
 (0)