- Notifications
You must be signed in to change notification settings - Fork269
JavaScript I without stretch questions#298
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:master
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,17 +5,30 @@ const counter = () => { | ||
| // Example: const newCounter = counter(); | ||
| // newCounter(); // 1 | ||
| // newCounter(); // 2 | ||
| let count = 0; | ||
| 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 count = 0; | ||
| return { | ||
| increment: () => ++count, | ||
| decrement: () => --count, | ||
| }; | ||
| }; | ||
| const limitFunctionCallCount = (cb, n) => { | ||
| // Should return a function that invokes `cb`. | ||
| // The returned function should only allow `cb` to be invoked `n` times. | ||
| let callCount = 0; | ||
| return (...args) => { | ||
| if (callCount === n) return null; | ||
| callCount++; | ||
| return cb(...args); | ||
| }; | ||
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 is exactly the solution code from the solution branch... In the future I'd like to see where you got with your own code. | ||
| }; | ||
| /* STRETCH PROBLEM */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -5,22 +5,26 @@ 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 | ||
| return Object.values(obj).map(key => obj[key]); | ||
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.
| ||
| }; | ||
| const mapObject = (obj, cb) => { | ||
| // Like map for arrays, but for objects. Transform the value of each property in turn. | ||
| // http://underscorejs.org/#mapObject | ||
| return Object.values(obj).map(key => cb(obj[key])); | ||
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. There's a few things wrong with this. | ||
| }; | ||
| const pairs = (obj) => { | ||
| // Convert an object into a list of [key, value] pairs. | ||
| // http://underscorejs.org/#pairs | ||
| return Object.keys(obj).map(key => [key, obj[key]]); | ||
| }; | ||
| /* STRETCH PROBLEMS */ | ||
Uh oh!
There was an error while loading.Please reload this page.