|
1 | | -// pass by reference |
| 1 | +// Complete the following functions. |
| 2 | +// These functions only need to work with arrays. |
| 3 | + |
| 4 | +/* eslint-disable no-unused-vars, max-len */ |
| 5 | + |
| 6 | +const each = (elements, cb) => { |
| 7 | + // Iterates over a list of elements, yielding each in turn to the `cb` function. |
| 8 | + // This only needs to work with arrays. |
| 9 | + // based off http://underscorejs.org/#each |
| 10 | +}; |
| 11 | + |
| 12 | +const map = (elements, cb) => { |
| 13 | + // Produces a new array of values by mapping each value in list through a transformation function (iteratee). |
| 14 | + // Return the new array. |
| 15 | +}; |
| 16 | + |
| 17 | +const reduce = (elements, cb, memo) => { |
| 18 | + // Combine all elements into a single value going from left to right. |
| 19 | + // Elements will be passed one by one into `cb`. |
| 20 | + // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value. |
| 21 | +}; |
| 22 | + |
| 23 | +const find = (elements, cb) => { |
| 24 | + // Look through each value in `elements` and pass each element to `cb`. |
| 25 | + // If `cb` returns `true` then return that element. |
| 26 | + // Return `undefined` if no elements pass the truth test. |
| 27 | +}; |
| 28 | + |
| 29 | +const filter = (elements, cb) => { |
| 30 | + // Similar to `find` but you will return an array of all elements that passed the truth test |
| 31 | + // Return an empty array if no elements pass the truth test |
| 32 | +}; |
| 33 | + |
| 34 | +const flatten = (elements) => { |
| 35 | + // Flattens a nested array (the nesting can be to any depth). |
| 36 | + // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; |
| 37 | +}; |
| 38 | + |
| 39 | +/* eslint-enable no-unused-vars, max-len */ |
| 40 | + |
| 41 | +module.exports = { |
| 42 | + each, |
| 43 | + map, |
| 44 | + reduce, |
| 45 | + find, |
| 46 | + filter, |
| 47 | + flatten, |
| 48 | +}; |
0 commit comments