Skip to content

Commit c2489d9

Browse files
committed
Project 18 complete
1 parent b43d304 commit c2489d9

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

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

Lines changed: 5 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,7 @@
181183
Video 58
182184
</li>
183185
</ul>
184-
<script>
185-
</script>
186+
<script src="typescripts.js"></script>
186187
</body>
187-
</html>
188+
189+
</html>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const timeNodes = Array.from(document.querySelectorAll('[data-time]'));
2+
console.time('map and reduce');
3+
const seconds = timeNodes
4+
.map((node) => node.dataset.time)
5+
.map(timeCode => {
6+
if (!timeCode)
7+
return 0;
8+
const [mins, secs] = timeCode.split(':').map(Number);
9+
return (mins * 60) + secs;
10+
})
11+
.reduce((total, vidSeconds) => total + vidSeconds);
12+
console.timeEnd('map and reduce');
13+
let secondsLeft = seconds;
14+
const hours = Math.floor(secondsLeft / 3600);
15+
secondsLeft = secondsLeft % 3600;
16+
const minutes = Math.floor(secondsLeft / 60);
17+
secondsLeft = secondsLeft % 60;
18+
console.log(`Map and reduce: ${hours}:${minutes}:${secondsLeft}`);
19+
console.time('just reduce');
20+
const redSeconds = timeNodes.reduce((totalSeconds, node) => {
21+
const [mins, secs] = node.dataset.time.split(':').map(Number);
22+
return totalSeconds + mins * 60 + secs;
23+
}, 0);
24+
console.timeEnd('just reduce');
25+
console.log(`Just reduce: ${Math.floor(redSeconds / 3600)}:${Math.floor((redSeconds % 3600) / 60)}:${redSeconds % 60}`);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* JavaScript30 by Wes Bos, https://javascript30.com/
3+
* TypeScript implementation by Will Wager
4+
* Project: Adding up times with reduce
5+
* Concepts: Map, reduce, calculating times
6+
* Key takeaways: Mapping step by step can be more readable, but combining
7+
* into a single map or reduce is less expensive.
8+
* Sidenotes: Targeted es6 to get Array.from
9+
* Compilation command:
10+
* tsc --removeComments --strictNullChecks --noImplicitAny --target es6 typescripts.ts
11+
*/
12+
13+
const timeNodes = Array.from(document.querySelectorAll('[data-time]'));
14+
console.time('map and reduce');
15+
const seconds = timeNodes
16+
.map((node: HTMLElement) => node.dataset.time)
17+
.map(timeCode => {
18+
if (!timeCode) return 0;
19+
const [mins, secs] = timeCode.split(':').map(Number);
20+
return (mins * 60) + secs;
21+
})
22+
.reduce((total, vidSeconds) => total + vidSeconds);
23+
console.timeEnd('map and reduce');
24+
25+
let secondsLeft = seconds;
26+
const hours = Math.floor(secondsLeft / 3600);
27+
secondsLeft = secondsLeft % 3600;
28+
const minutes = Math.floor(secondsLeft / 60);
29+
secondsLeft = secondsLeft % 60;
30+
console.log(`Map and reduce: ${hours}:${minutes}:${secondsLeft}`);
31+
32+
33+
console.time('just reduce');
34+
const redSeconds = timeNodes.reduce((totalSeconds: number, node: HTMLElement) => {
35+
const [mins, secs] = node.dataset.time!.split(':').map(Number);
36+
return totalSeconds + mins * 60 + secs;
37+
}, 0);
38+
console.timeEnd('just reduce');
39+
console.log(`Just reduce: ${Math.floor(redSeconds / 3600)}:${Math.floor((redSeconds % 3600) / 60)}:${redSeconds % 60}`);

0 commit comments

Comments
 (0)