-
Notifications
You must be signed in to change notification settings - Fork 269
tests passed #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests passed #1
Changes from all commits
6054e41
a903558
abf002b
9d4f70a
4c20b17
f229abb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,4 +17,4 @@ | |
| "no-useless-constructor": 0, | ||
| "import/no-unresolved": 0 | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| *.swp | ||
| node_modules | ||
| package-lock.json | ||
| *yarn* |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,37 +9,89 @@ const each = (elements, cb) => { | |
| // This only needs to work with arrays. | ||
| // You should also pass the index into `cb` as the second argument | ||
| // based off http://underscorejs.org/#each | ||
| for (let i = 0; i < elements.length; i++) { | ||
| cb(elements[i], i); | ||
| } | ||
| }; | ||
|
|
||
| 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. | ||
| }; | ||
|
|
||
| const reduce = (elements, cb, startingValue) => { | ||
| // Combine all elements into a single value going from left to right. | ||
| // Elements will be passed one by one into `cb`. | ||
| // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. | ||
| let i = 0; | ||
| if (startingValue === undefined) { | ||
| startingValue = elements[0]; | ||
| i = 1; | ||
| } | ||
| let acc = startingValue; | ||
| for (; i < elements.length; i++) { | ||
| acc = cb(acc, elements[i], i); | ||
| } | ||
| return acc; | ||
| }; | ||
|
|
||
| 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. | ||
| return reduce(elements, (acc, v) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Really creative
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||
| acc.push(cb(v)); | ||
| return acc; | ||
| }, []); | ||
| }; | ||
|
|
||
| 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. | ||
| for (let i = 0; i < elements.length; i++) { | ||
| if (cb(elements[i])) { | ||
| return elements[i]; | ||
| } | ||
| } | ||
| return undefined; | ||
| }; | ||
|
|
||
| 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 | ||
| return reduce(elements, (acc, v) => { | ||
| if (cb(v)) { | ||
| acc.push(v); | ||
| } | ||
| return acc; | ||
| }, []); | ||
| }; | ||
|
|
||
| /* Extra Credit */ | ||
|
|
||
| const isArray = (elements) => { | ||
| return Array.isArray(elements); | ||
| }; | ||
| const flatten = (elements) => { | ||
| // Flattens a nested array (the nesting can be to any depth). | ||
| // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; | ||
| if (!isArray(elements)) { | ||
| return elements; | ||
| } | ||
| while (elements.length === 1 && Array.isArray(elements[0])) { | ||
| elements = elements[0]; | ||
| } | ||
| let r = []; | ||
| r.push(flatten(elements[0])); | ||
| let next = elements.slice(1); | ||
| while (next.length === 1 && Array.isArray(next[0])) { | ||
| next = next[0]; | ||
| } | ||
| if (next.length > 0) { | ||
| while (r.length === 1 && Array.isArray(r[0])) { | ||
| r = r[0]; | ||
| } | ||
| r = r.concat(flatten(next)); | ||
| } | ||
| return r; | ||
| }; | ||
|
|
||
|
|
||
| /* eslint-enable no-unused-vars, max-len */ | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,47 @@ | ||
| // Complete the following underscore functions. | ||
| // Reference http://underscorejs.org/ for examples. | ||
| const each = require('./arrays.js').each; | ||
|
|
||
| const keys = (obj) => { | ||
| // Retrieve all the names of the object's properties. | ||
| // Return the keys as strings in an array. | ||
| // Based on http://underscorejs.org/#keys | ||
| return Object.keys(obj); | ||
| }; | ||
|
|
||
| function isFunction(func) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the sake of consistency this should be written the same way the other functions are written. |
||
| return (typeof func === 'function'); | ||
| } | ||
| const values = (obj) => { | ||
| // Return all of the values of the object's own properties. | ||
| // Ignore functions | ||
| // http://underscorejs.org/#values | ||
| const r = []; | ||
| each(keys(obj), (key) => { | ||
| if (!isFunction(obj[key])) { | ||
| r.push(obj[key]); | ||
| } | ||
| }); | ||
| return r; | ||
| }; | ||
|
|
||
| const mapObject = (obj, cb) => { | ||
| // Like map for arrays, but for objects. Transform the value of each property in turn. | ||
| // http://underscorejs.org/#mapObject | ||
| const r = {}; | ||
| each(keys(obj), (key) => { | ||
| r[key] = cb(obj[key]); | ||
| }); | ||
| return r; | ||
| }; | ||
|
|
||
| const pairs = (obj) => { | ||
| // Convert an object into a list of [key, value] pairs. | ||
| // http://underscorejs.org/#pairs | ||
| const r = []; | ||
| each(keys(obj), (key) => { | ||
| r.push([key, obj[key]]); | ||
| }); | ||
| return r; | ||
| }; | ||
|
|
||
| /* Extra credit */ | ||
|
|
@@ -29,12 +50,23 @@ const invert = (obj) => { | |
| // Returns a copy of the object where the keys have become the values and the values the keys. | ||
| // Assume that all of the object's values will be unique and string serializable. | ||
| // http://underscorejs.org/#invert | ||
| const r = {}; | ||
| each(keys(obj), (key) => { | ||
| r[obj[key]] = key; | ||
| }); | ||
| return r; | ||
| }; | ||
|
|
||
| const defaults = (obj, defaultProps) => { | ||
| // Fill in undefined properties that match properties on the `defaultProps` parameter object. | ||
| // Return `obj`. | ||
| // http://underscorejs.org/#defaults | ||
| each(keys(defaultProps), (key) => { | ||
| if (obj[key] === undefined) { | ||
| obj[key] = defaultProps[key]; | ||
| } | ||
| }); | ||
| return obj; | ||
| }; | ||
|
|
||
| /* eslint-enable no-unused-vars */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can also do this for a starting value:
(elements, cb, startingValue = elements.shift())for the default value. This takes advantage of the ES6 default parameters.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless of course you are avoiding modifying the input array