-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_hof_params_6.js
More file actions
63 lines (51 loc) · 1.45 KB
/
2_hof_params_6.js
File metadata and controls
63 lines (51 loc) · 1.45 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function saveSumOfFemaleAccounts() {
console.log(
reduce(
map(
filter(loadAccounts(), (person) => {return person.gender == 'f';}),
(p) => {
p.accountBalance *= 2;
return p;
}),
(sum, p) => { return sum + p.accountBalance; },
0)
);
}
function reduce(collection, reducer, accumulator) {
var retVal = accumulator;
for(let i=0; i<collection.length; i++) {
retVal = reducer(retVal, collection[i]);
}
return retVal;
}
function jsonCopy(src) {
return JSON.parse(JSON.stringify(src));
}
function map(collection, action) {
let colRet = [];
for(let i=0; i<collection.length; i++) {
let copy = jsonCopy(collection[i]);
colRet.push(action(copy));
}
return colRet;
}
function filter(collection, predicate) {
let colRet = [];
for(let i=0; i<collection.length; i++) {
if(predicate(collection[i])) {
colRet.push(collection[i]);
}
}
return colRet;
}
function loadAccounts() {
return [
{name: 'Alice', gender: 'f', birthYear: 1980, accountBalance: 1000},
{name: 'Bob', gender: 'm', birthYear: 1982, accountBalance: 1500},
{name: 'Carolina', gender: 'f', birthYear: 1986, accountBalance: 2000},
{name: 'Daniel', gender: 'm', birthYear: 1989, accountBalance: 2500},
{name: 'Epiphone', gender: 'f', birthYear: 1993, accountBalance: 3000},
{name: 'Frank', gender: 'm', birthYear: 1995, accountBalance: 3500}
];
}
saveSumOfFemaleAccounts();