From cd1a1f458f8a761ef561ee35c31bf44d1bc645da Mon Sep 17 00:00:00 2001 From: Alexander Turk Date: Sun, 4 Mar 2018 03:02:21 -0500 Subject: [PATCH] wipxsdo --- arrays.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/arrays.js | 11 +++++++---- 2 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 arrays.js diff --git a/arrays.js b/arrays.js new file mode 100644 index 0000000..732a8c2 --- /dev/null +++ b/arrays.js @@ -0,0 +1,53 @@ +// Complete the following functions. +// These functions only need to work with arrays. +// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc. +// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating +// You can use the functions that you have already written to help solve the other problems + +const each = (elements, cb) => { +for (let i = 0, i < elements.length, i++) { + cb(elements[i], i) +} +} + // Iterates over a list of elements, yielding each in turn to the `cb` function. + // This only needs to work with arrays. + // based off http://underscorejs.org/#each +}; + +const map = (elements, cb) => { + for (let i = 0; i < elements.length; i++) { + cb(elements[i], i); + } +const reduce = (elements, cb, memo = elements.shift()) => { + // Combine all elements into a single value going from left to right. + // Elements will be passed one by one into `cb`. + // `memo` is the starting value. If `memo` is undefined then make `elements[0]` the initial value. +}; + +const find = (elements, cb) => { + // Look through each value in `elements` and pass each element to `cb`. + // If `cb` returns `true` then return that element. + // Return `undefined` if no elements pass the truth test. +}; + +const filter = (elements, cb) => { + // Similar to `find` but you will return an array of all elements that passed the truth test + // Return an empty array if no elements pass the truth test +}; + +/* Extra Credit */ +const flatten = (elements) => { + // Flattens a nested array (the nesting can be to any depth). + // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; +}; + +/* eslint-enable no-unused-vars, max-len */ + +module.exports = { + each, + map, + reduce, + find, + filter, + flatten +}; diff --git a/src/arrays.js b/src/arrays.js index b995952..732a8c2 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -5,16 +5,19 @@ // You can use the functions that you have already written to help solve the other problems const each = (elements, cb) => { +for (let i = 0, i < elements.length, i++) { + cb(elements[i], i) +} +} // Iterates over a list of elements, yielding each in turn to the `cb` function. // This only needs to work with arrays. // based off http://underscorejs.org/#each }; const map = (elements, cb) => { - // Produces a new array of values by mapping each value in list through a transformation function (iteratee). - // Return the new array. -}; - + for (let i = 0; i < elements.length; i++) { + cb(elements[i], i); + } const reduce = (elements, cb, memo = elements.shift()) => { // Combine all elements into a single value going from left to right. // Elements will be passed one by one into `cb`.