File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ====
917const 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} ;
You can’t perform that action at this time.
0 commit comments