Skip to content

Commit 537ddc7

Browse files
Leana NeparidzeLeana Neparidze
authored andcommitted
completes closure
1 parent 04572cb commit 537ddc7

1 file changed

Lines changed: 25 additions & 1 deletion

File tree

assignments/closure.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
// ==== Challenge 1: Write your own closure ====
22
// Write a simple closure of your own creation. Keep it simple!
3-
3+
const closureFunc = () => {
4+
let arr = ['a', 'b', 'c'];
5+
return function(array) {
6+
arr = arr.concat(array);
7+
return arr;
8+
}
9+
}
10+
//let func = closureFunc();
11+
//console.log(func(['d']));
412

513
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
614

715

816
// ==== Challenge 2: Create a counter function ====
917
const counter = () => {
1018
// Return a function that when invoked increments and returns a counter variable.
19+
var counter = 0;
20+
function foo() {
21+
counter = counter + 1;
22+
return counter;
23+
}
24+
return foo;
1125
};
1226
// Example usage: const newCounter = counter();
1327
// newCounter(); // 1
@@ -18,4 +32,14 @@ const counterFactory = () => {
1832
// Return an object that has two methods called `increment` and `decrement`.
1933
// `increment` should increment a counter variable in closure scope and return it.
2034
// `decrement` should decrement the counter variable and return it.
35+
var counter = 0;
36+
var obj = {
37+
"increment": function() {
38+
return counter += 1;
39+
},
40+
"decrement": function() {
41+
return counter -= 1;
42+
}
43+
}
44+
return obj;
2145
};

0 commit comments

Comments
 (0)