Skip to content

Commit 18b8a55

Browse files
MINEMINE
authored andcommitted
Completed Closure thru Googling
1 parent cd2e299 commit 18b8a55

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/closure.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,43 @@ const counter = () => {
55
// Example: const newCounter = counter();
66
// newCounter(); // 1
77
// newCounter(); // 2
8+
let count = 0;
9+
return () => {
10+
count++;
11+
return count;
12+
};
813
};
914

1015
const 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+
this.counter = count;
24+
return this.counter;
25+
},
26+
decrement() {
27+
count--;
28+
this.counter = count;
29+
return this.counter;
30+
},
31+
};
1432
};
1533

1634
const limitFunctionCallCount = (cb, n) => {
1735
// Should return a function that invokes `cb`.
1836
// The returned function should only allow `cb` to be invoked `n` times.
37+
let limit = 0;
38+
return (...args) => {
39+
if (limit === n) {
40+
return null;
41+
}
42+
limit += 1;
43+
return cb(...args);
44+
};
1945
};
2046

2147
/* STRETCH PROBLEM */
@@ -27,6 +53,12 @@ const cacheFunction = (cb) => {
2753
// If the returned function is invoked with arguments that it has already seen
2854
// then it should return the cached result and not invoke `cb` again.
2955
// `cb` should only ever be invoked once for a given set of arguments.
56+
const cache = {};
57+
return (args) => {
58+
if (Object.prototype.hasOwnProperty.call(cache, args)) return cache[args];
59+
cache[args] = cb(args);
60+
return cache[args];
61+
};
3062
};
3163

3264
/* eslint-enable no-unused-vars */

src/objects.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ const keys = (obj) => {
55
// Retrieve all the names of the object's properties.
66
// Return the keys as strings in an array.
77
// Based on http://underscorejs.org/#keys
8-
return Object.keys(obj);
8+
// return Object.keys(obj);
9+
const property = Object.keys(obj);
10+
return property;
911
};
1012

1113
const values = (obj) => {
1214
// Return all of the values of the object's own properties.
1315
// Ignore functions
1416
// http://underscorejs.org/#values
15-
return Object.values(obj);
17+
// return Object.values(obj);
18+
const valueOfProperty = Object.values(obj);
19+
return valueOfProperty;
1620
};
1721

1822
const mapObject = (obj, cb) => {
@@ -27,6 +31,7 @@ const mapObject = (obj, cb) => {
2731
const pairs = (obj) => {
2832
// Convert an object into a list of [key, value] pairs.
2933
// http://underscorejs.org/#pairs
34+
return Object.entries(obj);
3035
};
3136

3237
/* STRETCH PROBLEMS */
@@ -35,6 +40,11 @@ const invert = (obj) => {
3540
// Returns a copy of the object where the keys have become the values and the values the keys.
3641
// Assume that all of the object's values will be unique and string serializable.
3742
// http://underscorejs.org/#invert
43+
const newObj = {};
44+
Object.keys(obj).forEach((key) => {
45+
newObj[obj[key]] = key;
46+
});
47+
return newObj;
3848
};
3949

4050
const defaults = (obj, defaultProps) => {

yarn-error.log

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Arguments:
2-
/usr/local/bin/node /usr/local/bin/yarn test objects
2+
/usr/local/bin/node /usr/local/bin/yarn test closure
33

44
PATH:
55
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
@@ -3587,7 +3587,7 @@ Trace:
35873587
Error: Command failed.
35883588
Exit code: 1
35893589
Command: sh
3590-
Arguments: -c eslint tests/*.js && eslint src/*.js && jest --verbose objects
3590+
Arguments: -c eslint tests/*.js && eslint src/*.js && jest --verbose closure
35913591
Directory: /Users/Mine/LambdaSchool/JavaScript-I
35923592
Output:
35933593

0 commit comments

Comments
 (0)