File tree Expand file tree Collapse file tree 1 file changed +33
-1
lines changed
Expand file tree Collapse file tree 1 file changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -4,18 +4,42 @@ const counter = () => {
44 // Return a function that when invoked increments and returns a counter variable.
55 // Example: const newCounter = counter();
66 // newCounter(); // 1
7- // newCounter(); // 2
7+ // console.log(...arg);
8+ let count = 0 ;
9+ return ( ) => {
10+ count ++ ;
11+ return count ;
12+ } ;
813} ;
914
1015const counterFactory = ( ) => {
1116 // Return an object that has two methods called `increment` and `decrement`.
1217 // `increment` should increment a counter variable in closure scope and return it.
1318 // `decrement` should decrement the counter variable and return it.
19+ let count = 0 ;
20+ return {
21+ increment : ( ) => {
22+ count ++ ;
23+ return count ;
24+ } ,
25+ decrement : ( ) => {
26+ count -- ;
27+ return count ;
28+ } ,
29+ } ;
1430} ;
1531
1632const limitFunctionCallCount = ( cb , n ) => {
1733 // Should return a function that invokes `cb`.
1834 // The returned function should only allow `cb` to be invoked `n` times.
35+ let count = 0 ;
36+ return ( ...args ) => {
37+ if ( count === n ) {
38+ return null ;
39+ }
40+ count ++ ;
41+ return cb ( ...args ) ;
42+ } ;
1943} ;
2044
2145/* STRETCH PROBLEM */
@@ -27,6 +51,14 @@ const cacheFunction = (cb) => {
2751 // If the returned function is invoked with arguments that it has already seen
2852 // then it should return the cached result and not invoke `cb` again.
2953 // `cb` should only ever be invoked once for a given set of arguments.
54+ const cache = { } ;
55+ return ( arg ) => {
56+ if ( arg in cache ) {
57+ return cache [ arg ] ;
58+ }
59+ cache [ arg ] = cb ;
60+ return cb ( arg ) ;
61+ } ;
3062} ;
3163
3264/* eslint-enable no-unused-vars */
You can’t perform that action at this time.
0 commit comments