-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·148 lines (109 loc) · 3.69 KB
/
main.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
var api = {};
/**
PART 1: Implement fanOut.
fanOut - return a new collection of results after applying the
input function to each item in the input collection.
args: input - input collection
fn - function to apply to each item in the collection
EX: - fanOut([1, 2, 3], double) --> [1, 4, 9];
function double(n) { return n * n; }
Restrictions:
- Do not use make any function calls (other than fn and push)
- You may not use any external libraries
*/
api.fanOut = (input, fn) => {
/**
* Your implementation goes here
*/
const arr = [];
for (const element of input)
arr.push(fn(element));
return arr;
};
/**
PART 2: Implement funnel.
funnel - return an result after applying an accumulation function to
each item in the collection. Funneling down to a single result.
args: input - input collection
fn - function to apply to each item in the collection with
args accumulation value and current value
startValue - start the accumulation with this value
EX: - funnel([1, 2, 3], add, 0) --> 6;
- funnel([1, 2], add, 1) --> 4
function add(total, n) { return total + n; }
Restrictions:
- Do not use make any function calls (other than fn and push)
- You may not use any external libraries
*/
api.funnel = (input, fn, startValue) => {
/**
* Your implementation goes here
*/
var accumulator = startValue;
for (const element of input)
accumulator = fn(accumulator, element);
return accumulator;
};
/**
PART 3: Implement distill.
distill - return a new collection of results after applying the
predicate function to each item. Only include the item in the result
if the predicate returns true.
args: input - input collection
fn - predicate function to apply to each item in the collection
EX: - distill([1, 2, 3], isEven) --> [2];
- distill([1, 2, 3], isOdd) --> [1, 3];
- distill([1, 2, 3], isNegative) --> [];
Restrictions:
- Do not use make any function calls (other than fn and push)
- You may not use any external libraries
*/
api.distill = (input, fn) => {
/**
* Your implementation goes here
*/
const arr = [];
for (const element of input)
if(fn(element))
arr.push(element);
return arr;
};
/**
PART 4: Implement numberOfChars.
numberOfChars - return the number of characters in the input array of strings
args: input - input collection of strings (words)
EX: - numberOfChars(['the']) --> 3;
- numberOfChars(['the', 'end']) --> 6;
Restrictions:
- You MAY use fanOut, funnel, and distill, and the length property
- You may not use make any other function calls
- You may not use any external libraries
*/
api.numberOfChars = (input) => {
/**
* Your implementation goes here
*/
const fnCharCount = (total, element) => total + element.length;
return api.funnel(input, fnCharCount, 0);
};
/**
PART 5: Implement numberOfCertainChars.
numberOfCertainChars - return the number of c characters in the input array of strings
args: input - input collection of strings (words)
c - the certain character to count
EX: - numberOfCertainChars(['the'], 'e') --> 1;
- numberOfCertainChars(['the', 'end'], 'e') --> 2;
Restrictions:
- You MAY use fanOut, funnel, and distill, and the length property
- You may not use make any other function calls
- You may not use any external libraries
*/
api.numberOfCertainChars = (input, c) => {
/**
* Your implementation goes here
*/
const fnFilterChars = word => api.distill(word, tempChar => tempChar === c );
const fnCharCount = (total, element) => total + element.length;
return api.funnel(api.fanOut(input, fnFilterChars), fnCharCount, 0);
};
module.exports = api;