-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
359 lines (336 loc) · 11.2 KB
/
index.js
File metadata and controls
359 lines (336 loc) · 11.2 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// ⭐️ Example Challenge START ⭐️
/**
* ### Challenge `processFirstItem`
*
* @instructions
* Implement a higher-order function called `processFirstItem`.
* It takes two arguments:
* @param stringList an array of strings.
* @param callback function that takes a string as its argument.
* @returns the result of invoking `callback` with the FIRST element in `stringList`.
*
* Example of usage of this higher-order function:
* Invoking `processFirstItem` passing `['foo', 'bar']` and `(str) => str + str`,
* should return 'foofoo'.
*/
function processFirstItem(stringList, callback) {
return callback(stringList[0]);
}
// ⭐️ Example Challenge END ⭐️
// 👇 COMPLETE YOUR WORK BELOW 👇
// 👇 COMPLETE YOUR WORK BELOW 👇
// 👇 COMPLETE YOUR WORK BELOW 👇
/////////////// CALLBACKS AND HIGHER-ORDER FUNCTIONS ///////////////
/////////////// CALLBACKS AND HIGHER-ORDER FUNCTIONS ///////////////
/**
* ### Challenge `processLength`
*
* @instructions
* PLEASE STUDY THE EXAMPLE CHALLENGE THOROUGHLY BEFORE PROCEEDING!
* PLEASE STUDY THE EXAMPLE CHALLENGE THOROUGHLY BEFORE PROCEEDING!
*
* Implement a higher-order function called `processLength`.
* It takes two arguments:
* @param list an array with elements of any type.
* @param callback function that takes a number as its argument.
* @returns the result of invoking `callback` passing the LENGTH of `list`.
*
* Examples of usage of this higher-order function:
* [1] Invoking `processLength` passing `['foo', 'bar']` and `(num) => num + 1000`,
* should return 1002.
*
* [2] Invoking `processLength` passing `[]` and `(num) => "There are " + num`,
* should return "There are 0".
*/
function processLength(list, numCallBack) {
return numCallBack(list.length);
/* CODE HERE */
}
/**
* ### Challenge `processLastItem`
*
* @instructions
* Implement a higher-order function called `processLastItem`.
* It takes two arguments:
* @param stringList array of strings.
* @param callback function that takes a string as its argument.
* @returns the result of invoking `callback` with the LAST element in `stringList`.
*
* Example of usage of this higher-order function:
* Invoking `processLastItem` passing `['foo', 'bar']` and `(str) => str + str`,
* should return 'barbar'.
*/
function processLastItem(stringList, callBack) {
return callBack(stringList[stringList.length - 1]);
}
/**
* ### Challenge `processSum`
*
* @instructions
* Implement a higher-order function called `processSum`.
* It takes two arguments:
* @param numberList array of numbers.
* @param callback function that takes a number as its argument.
* @returns the result of invoking `callback` passing the SUM of all elements in `numberList`.
*
* Examples of usage of this higher-order function:
* [1] Invoking `processSum` passing `[10, 20, 30]` and `(num) => num + " is a big number!"`,
* should return "60 is a big number!".
*
* [2] Invoking `processSum` passing `[]` and `(num) => num + 1000`,
* should return 1000.
*/
function processSum(numberList, sumCallBack) {
let allNum = numberList.reduce((total, currValue) => {
return total + currValue;
}, 0);
return sumCallBack(allNum);
}
/**
* ### Challenge `processProduct`
*
* @instructions
* Implement a higher-order function called `processProduct`.
* It takes three arguments:
* @param num1 a number.
* @param num2 a number.
* @param callback function that takes a number as its argument.
* @returns the result of invoking `callback` passing the PRODUCT of `num1` and `num2`.
*
* Examples of usage of this higher-order function:
* [1] Invoking `processProduct` passing 2 and 7 and `(num) => num + " is a big number!"`,
* should return "14 is a big number!".
*
* [2] Invoking `processProduct` passing 25 and 0 and `(num) => num + 1000`,
* should return 1000.
*/
function processProduct(num1, num2, callback) {
const product = num1 * num2;
return callback(product);
}
/**
* ### Challenge `processContains`
*
* @instructions
* Implement a higher-order function called `processContains`.
* It takes three arguments:
* @param item of any kind.
* @param list array of elements of any kind.
* @param callback function that takes a boolean as its argument.
* @returns the result of invoking `callback` passing true if `item` exists in `list`, false otherwise.
*
* Examples of usage of this higher-order function:
* [1] Invoking `processContains` passing
* "foo" and `['foo', 'bar']` and `(bool) => bool ? 'nice!' : 'sad'`
* should return "nice!".
*
* [2] Invoking `processContains` passing
* "lady gaga" and `['foo', 'bar']` and `(bool) => bool ? 'nice!' : 'sad'`,
* should return "sad".
*/
function processContains(item, list, callback) {
if (list.includes(item)) {
return callback(true);
}
return callback(false);
}
/**
* ### Challenge `processDuplicateFree`
* THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES!
* THIS IS A STRETCH PROBLEM! ATTEMPT ONLY AFTER COMPLETING ALL NON-STRETCH CHALLENGES!
*
* @instructions
* Implement a higher-order function called `processDuplicateFree`.
* It takes two arguments:
* @param list array of elements of any kind.
* @param callback function that takes an array as its argument.
* @returns the result of invoking `callback` passing a de-duped version of `list`.
*
* Examples of usage of this higher-order function:
* [1] Invoking `processDuplicateFree` passing `[1,1,1,2]` and `(arr) => arr`,
* should return `[1,2]`.
*
* [2] Invoking `processDuplicateFree` passing `[1,1,2,2,3]` and `(arr) => arr.length`,
* should return 3.
*/
function processDuplicateFree(list, callback) {
return callback(list.filter((a, b) => list.indexOf(a) === b));
/* CODE HERE ONLY AFTER COMPLETING ALL OTHER TASKS */
}
/////////////// HIGHER-ORDER ARRAY METHODS ///////////////
/////////////// HIGHER-ORDER ARRAY METHODS ///////////////
// A local community center is holding a fund raising 5k fun run and has invited
// 50 small businesses to make a small donation on their behalf for some much needed
// updates to their facilities. Each business has assigned a representative
// to attend the event along with a small donation.
/**
* ### Challenge `getFullNames`
*
* @instructions
* Implement this function using forEach().
*
* @param runners array of runners like the one inside the /data/runners.js file.
* @returns an array with all the runners' full names in the following format: "Smith, John".
* The full names appear in the array in the same order the runners appear in the `runners` array.
*/
function getFullNames(runners) {
let runnerNames = [];
runners.forEach(function(runner) {
return runnerNames.push(`${runner.last_name}, ${runner.first_name}`);
});
return runnerNames;
}
/**
* ### Challenge `firstNamesAllCaps`
*
* @instructions
* The event director needs to have all the runners' first names
* in uppercase because the director BECAME DRUNK WITH POWER.
* Implement this function using map().
*
* @param runners array of runners like the one inside the /data/runners.js file.
* @returns an array with all the runners' first names in ALL CAPS.
* The first names appear in the array in the same order the runners appear in the `runners` array.
*/
function firstNamesAllCaps(runners) {
const newMapArray = [];
runners.map(function(runner) {
return newMapArray.push(runner.first_name.toUpperCase());
});
return newMapArray;
}
/**
* ### Challenge `getRunnersByTShirtSize`
*
* @instructions
* The event director needs a way to find the runners that need
* a specific t-shirt size, so they can place the orders easily.
* Implement this function using filter().
*
* @param runners array of runners like the one inside the /data/runners.js file.
* @param tShirtSize string (possible values are "S", "M", "L", "XL", "2XL", "3XL").
* @returns an array containing only the runners that use the given `tShirtSize`.
* The runners in the array appear in the same order they appear in the `runners` array.
*/
function getRunnersByTShirtSize(runners, tShirtSize) {
const array = runners.filter(runner => runner.shirt_size === tShirtSize);
return array;
}
/**
* ### Challenge `tallyUpDonations`
*
* @instructions
* The donations need to be tallied up and reported for tax purposes.
* Implement this function using reduce().
*
* @param runners array of runners like the one inside the /data/runners.js file.
* @returns a number which is the sum of the donations by all runners.
*/
function tallyUpDonations(runners) {
let donationTotal = runners.reduce((total, runner) => {
return total + runner.donation;
}, 0);
return donationTotal;
}
/////////////// CLOSURES ///////////////
/////////////// CLOSURES ///////////////
/**
* ### Challenge `counterMaker`
*
* @instructions
* Fix this function so a counter produced with it will increment correctly!
* Usage is as follows:
*
* const counter = counterMaker()
* counter() // should return 0
* counter() // should return 1
* counter() // should return 2
* etc
*/
function counterMaker() {
let count = 0;
function counter() {
return count++;
}
return counter;
}
/**
* ### Challenge `counterMakerWithLimit`
*
* @instructions
* Implement a counter maker that takes a max value for the count.
* A counter created with it will reset itself after reaching the max value.
* Usage is as follows:
*
* const counter = counterMakerWithLimit(3)
* counter() // should return 0
* counter() // should return 1
* counter() // should return 2
* counter() // should return 3
* counter() // should return 0
* counter() // should return 1
* counter() // should return 2
* counter() // should return 3
* counter() // should return 0
* etc
*/
function counterMakerWithLimit(maxValue) {
let count = 0;
function counter() {
if (count <= maxValue) {
return count++;
}
if (count > maxValue) {
count = 0;
return count++;
}
}
return counter;
}
/////////////// END OF CHALLENGE ///////////////
/////////////// END OF CHALLENGE ///////////////
/////////////// END OF CHALLENGE ///////////////
if (typeof exports !== "undefined") {
// IGNORE: Test/Env Detected
// For Node/Non-browser test env
module.exports = module.exports || {};
if (processFirstItem) {
module.exports.processFirstItem = processFirstItem;
}
if (processLength) {
module.exports.processLength = processLength;
}
if (processLastItem) {
module.exports.processLastItem = processLastItem;
}
if (processSum) {
module.exports.processSum = processSum;
}
if (processProduct) {
module.exports.processProduct = processProduct;
}
if (processContains) {
module.exports.processContains = processContains;
}
if (processDuplicateFree) {
module.exports.processDuplicateFree = processDuplicateFree;
}
if (getFullNames) {
module.exports.getFullNames = getFullNames;
}
if (firstNamesAllCaps) {
module.exports.firstNamesAllCaps = firstNamesAllCaps;
}
if (getRunnersByTShirtSize) {
module.exports.getRunnersByTShirtSize = getRunnersByTShirtSize;
}
if (tallyUpDonations) {
module.exports.tallyUpDonations = tallyUpDonations;
}
if (counterMaker) {
module.exports.counterMaker = counterMaker;
}
if (counterMakerWithLimit) {
module.exports.counterMakerWithLimit = counterMakerWithLimit;
}
}