Repository navigation
Solutions for Advanced-JavaScript #55
Changes from all commits
4116799
11de9b1
108e6ce
5974f42
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 |
|---|---|---|
|
|
@@ -8,36 +8,111 @@ const each = (elements, cb) => { | |
| // 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 | ||
|
|
||
| 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. | ||
|
|
||
| // declare result array object to be returned. | ||
| const result = []; | ||
| // loop over the elements | ||
| for (let i = 0; i < elements.length; i++) { | ||
| // invoke cb function on elements | ||
| result.push(cb(elements[i])); | ||
| // push result to result array | ||
| } | ||
| // return result array | ||
| return result; | ||
| }; | ||
|
|
||
| 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. | ||
|
|
||
| // cb(memo, elements[0]); | ||
| let result = memo; | ||
| // const initialValue = memo; | ||
|
|
||
| if (elements.length > 1) { | ||
| // Loop over the array | ||
| for (let i = 0; i < elements.length; i++) { | ||
| // call the cb function on current element in elements. | ||
| result = cb(result, elements[i]); | ||
| // call shift on elements | ||
| // save returned value to memo. | ||
| // memo = result; | ||
| } | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| 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. | ||
|
|
||
| // Loop through elements array. | ||
| for (let i = 0; i < elements.length; i++) { | ||
| // Check truth statement of the callback. | ||
| // Return the value when the callback is true. | ||
| if (cb(elements[i])) { | ||
| return elements[i]; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| 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 | ||
| // Create a result array that stores all the values that will be returned. | ||
|
|
||
| const result = []; | ||
| // Loop Through the elements array. | ||
| for (let i = 0; i < elements.length; i++) { | ||
| // Push elements that pass the test to the result array. | ||
| if (cb(elements[i])) { | ||
| result.push(elements[i]); | ||
| } | ||
| } | ||
| // Return the result array. | ||
| return result; | ||
| }; | ||
|
|
||
| /* 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]; | ||
| }; | ||
| let result = []; | ||
|
|
||
| function getValues(element, res = []) { | ||
|
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. Nice default in the parens. |
||
| // loop over all elements in array | ||
| for (let i = 0; i < element.length; i++) { | ||
| if (typeof element[i] === 'object') { | ||
| res = getValues(element[i], result); | ||
| } else { | ||
| res.push(element[i]); | ||
| } | ||
| } | ||
| return res; | ||
| } | ||
|
|
||
| for (let i = 0; i < elements.length; i++) { | ||
|
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. Not sure what's going on with two different loops here. There may be a better way to solve this. Please refer to the video posted about flatten and reduce, it's up in Week 1 on your piazza account. |
||
| if (typeof elements[i] === 'object') { | ||
| result = getValues(elements[i], result); | ||
| } else { | ||
| result.push(elements[i]); | ||
| } | ||
| } | ||
|
|
||
| // result = elements.map(getValues); | ||
| return result; | ||
| }; | ||
| /* eslint-enable no-unused-vars, max-len */ | ||
|
|
||
| module.exports = { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,38 @@ | |
| // `meow` that should return the string `<name> meowed!` where `<name>` is the `name` | ||
| // property set on the Cat instance. | ||
|
|
||
| class User { | ||
|
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. This all looks really good. It seems like you have a good grasp on these concepts here. |
||
| constructor(options) { | ||
| this.email = options.email; | ||
| this.password = options.password; | ||
| } | ||
|
|
||
| comparePasswords(password) { | ||
| return (password === this.password); | ||
| } | ||
| } | ||
|
|
||
| class Animal { | ||
| constructor(options) { | ||
| this.age = options.age; | ||
| } | ||
|
|
||
| growOlder() { | ||
| this.age++; | ||
| return this.age; | ||
| } | ||
| } | ||
|
|
||
| class Cat extends Animal { | ||
| constructor(options) { | ||
| super(options); | ||
| this.name = options.name; | ||
| } | ||
|
|
||
| meow() { | ||
| return `${this.name} meowed!`; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| User, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,17 +5,45 @@ const counter = () => { | |
| // Example: const newCounter = counter(); | ||
| // newCounter(); // 1 | ||
| // newCounter(); // 2 | ||
| let count = 0; | ||
| return () => { | ||
| count++; | ||
| return count; | ||
| }; | ||
| }; | ||
|
|
||
| const counterFactory = () => { | ||
| // Return an object that has two methods called `increment` and `decrement`. | ||
| // `increment` should increment a counter variable in closure scope and return it. | ||
| // `decrement` should decrement the counter variable and return it. | ||
| let counter1 = 0; | ||
|
|
||
| return { | ||
| increment: () => { | ||
| counter1++; | ||
| return counter1; | ||
| }, | ||
| decrement: () => { | ||
| counter1--; | ||
| return counter1; | ||
| } | ||
| }; | ||
| }; | ||
|
|
||
| const limitFunctionCallCount = (cb, n) => { | ||
| // Should return a function that invokes `cb`. | ||
| // The returned function should only allow `cb` to be invoked `n` times. | ||
| let count = 0; | ||
|
|
||
| return (...val) => { | ||
| count++; | ||
|
|
||
|
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. No need for this space right here. |
||
| if (count < n) { | ||
| return cb(...val); | ||
| } | ||
|
|
||
|
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. No need for this space here. |
||
| return null; | ||
| }; | ||
| }; | ||
|
|
||
| /* Extra Credit */ | ||
|
|
@@ -26,6 +54,19 @@ const cacheFunction = (cb) => { | |
| // If the returned function is invoked with arguments that it has already seen | ||
| // then it should return the cached result and not invoke `cb` again. | ||
| // `cb` should only ever be invoked once for a given set of arguments. | ||
| const arg = []; | ||
| const result = []; | ||
| return (unknown) => { | ||
| if (arg.indexOf(unknown) !== -1) { | ||
| return result[arg.indexOf(unknown)]; | ||
|
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. Nice work. I really like indexOf. And I like that you used an array to cache your function call. I would rename this array to something more descriptive other than 'arg' just to make sure if someone was to re-read your code, they'd be able to understand exactly what 'arg' is. |
||
| } | ||
| const current = cb(unknown); | ||
|
|
||
| arg.push(unknown); | ||
| result.push(current); | ||
|
|
||
| return current; | ||
| }; | ||
| }; | ||
|
|
||
| /* eslint-enable no-unused-vars */ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,34 +5,94 @@ 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); | ||
| }; | ||
|
|
||
| const values = (obj) => { | ||
| // Return all of the values of the object's own properties. | ||
| // Ignore functions | ||
| // http://underscorejs.org/#values | ||
| // http://underscorejs.org/# | ||
|
|
||
| const objKeys = Object.keys(obj); | ||
| const result = []; | ||
|
|
||
| objKeys.forEach((k) => { | ||
| return result.push(obj[k]); | ||
| }); | ||
|
|
||
| // for (const key in obj) { | ||
|
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. You can just deleted code that you've commented out. It makes for a cleaner submission. |
||
| // if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
| // result.push(obj[key]); | ||
| // } | ||
| // } | ||
| return result; | ||
| }; | ||
|
|
||
| const mapObject = (obj, cb) => { | ||
| // Like map for arrays, but for objects. Transform the value of each property in turn. | ||
| // http://underscorejs.org/#mapObject | ||
| const objKeys = Object.keys(obj); | ||
| const returnObj = Object.assign({}, obj); | ||
|
|
||
| objKeys.forEach((k) => { | ||
| returnObj[k] = cb(obj[k]); | ||
| }); | ||
|
|
||
| return returnObj; | ||
| }; | ||
|
|
||
| const pairs = (obj) => { | ||
| // Convert an object into a list of [key, value] pairs. | ||
| // http://underscorejs.org/#pairs | ||
|
|
||
| const objKeys = Object.keys(obj); | ||
| const result = []; | ||
|
|
||
| objKeys.forEach((k) => { | ||
| result.push([k, obj[k]]); | ||
| }); | ||
|
|
||
| return result; | ||
| }; | ||
|
|
||
| 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 objKeys = Object.keys(obj); | ||
| const returnObj = {}; | ||
|
|
||
| objKeys.forEach((k) => { | ||
| returnObj[obj[k]] = k; | ||
| }); | ||
|
|
||
| return returnObj; | ||
| }; | ||
|
|
||
| const defaults = (obj, defaultProps) => { | ||
| // Fill in undefined properties that match properties on the `defaultProps` parameter object. | ||
| // Return `obj`. | ||
| // http://underscorejs.org/#defaults | ||
| function hasKey(o, ky) { | ||
| // const objKeys = Object.keys(o); | ||
|
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. please remove this commented out code |
||
| // return objKeys.includes(ky) || o[ky] !== undefined; | ||
| return o[ky] === undefined; | ||
| } | ||
|
|
||
| const defaultKeys = Object.keys(defaultProps); | ||
|
|
||
| defaultKeys.forEach((k) => { | ||
| if (hasKey(obj, k)) { | ||
| obj[k] = defaultProps[k]; | ||
| } | ||
| }); | ||
|
|
||
| // for (const key in objKeys) { | ||
| // if (obj[key] === undefined && defaultProps.hasKey(key)) { | ||
| // 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're good to reuse your
.each()method here. Maybe you could give it a try.