-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexercise02.html
More file actions
30 lines (29 loc) · 826 Bytes
/
exercise02.html
File metadata and controls
30 lines (29 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Exercise #2</title>
<script type="text/javascript">
const numbers = [4, 8, 15, 16, 23, 42];
// even -> cube -> sum
let sum = 0;
for (let number of numbers){
if (number%2 === 0){
const cubed = number ** 3;
sum += cubed;
}
}
console.log(`sum is ${sum}.`);
const even_numbers = n => n%2 === 0;
const to_cube = u => u*u*u;
const to_sum = (s,n) => s+n;
// functional programming
sum = numbers.filter(even_numbers)
.map( to_cube )
.reduce(to_sum, 0);
console.log(`sum is ${sum}.`);
</script>
</head>
<body>
</body>
</html>