33// Keep it simple! Remember a closure is just a function
44// that manipulates variables defined in the outer scope.
55// The outer scope can be a parent function, or the top level of the script.
6+ function drawPie ( ) {
7+ return JSON . stringify ( " (" ) + "\n" + JSON . stringify ( " ( ) )" )
8+ + "\n" + JSON . stringify ( " ) ( )" ) + "\n" + JSON . stringify ( " ....." )
9+ + "\n" + JSON . stringify ( " .:::::::::." ) + "\n" + JSON . stringify ( "~\\_______/~" ) + "\n" ;
10+ }
11+ function piMaker ( numPis ) {
12+ let pi = Math . PI ;
13+ function addX ( ) {
14+ for ( let i = 0 ; i < numPis ; i ++ ) {
15+ console . log ( i + " " + drawPie ( ) ) ;
16+ }
17+ return numPis * pi ;
18+ }
19+ return addX ( pi ) ;
20+ } ;
21+ console . log ( "Here's my closure" ) ;
22+ console . log ( piMaker ( 3 ) ) ;
623
724
825/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
@@ -16,18 +33,65 @@ const counterMaker = () => {
1633 // NOTE: This `counter` function, being nested inside `counterMaker`,
1734 // "closes over" the `count` variable. It can "see" it in the parent scope!
1835 // 3- Return the `counter` function.
19- } ;
36+ let count = 0 ;
37+ function counter ( ) {
38+ count ++ ;
39+ return count ;
40+ }
41+ return counter ;
42+ } ;
2043// Example usage: const myCounter = counterMaker();
44+ const myCounter = counterMaker ( ) ;
2145// myCounter(); // 1
46+ console . log ( "myCounter: " + myCounter ( ) ) ;
47+ console . log ( "myCounter: " + myCounter ( ) ) ;
48+ console . log ( "myCounter: " + myCounter ( ) ) ;
2249// myCounter(); // 2
2350
2451// ==== Challenge 3: Make `counterMaker` more sophisticated ====
2552// It should have a `limit` parameter. Any counters we make with `counterMaker`
2653// will refuse to go over the limit, and start back at 1.
54+ const counterMaker2 = ( limit ) => {
55+ let count = 0 ;
56+ function counter ( ) {
57+ count ++ ;
58+ if ( count > limit ) {
59+ count = 1 ;
60+ }
61+ return count ;
62+ }
63+ return counter ;
64+ }
65+
66+ const myCounter2 = counterMaker2 ( 3 ) ;
67+ // myCounter(); // 1
68+ console . log ( "myCounter2: " + myCounter2 ( ) ) ;
69+ console . log ( "myCounter2: " + myCounter2 ( ) ) ;
70+ console . log ( "myCounter2: " + myCounter2 ( ) ) ;
71+ console . log ( "myCounter2: " + myCounter2 ( ) ) ;
72+ console . log ( "myCounter2: " + myCounter2 ( ) ) ;
2773
28- // ==== Challenge 4: Create a counter function with an object that can increment and decrement ====
74+ // ==== Challenge 4: Create a counter function with an object that can increment
75+ // and decrement ====
2976const counterFactory = ( ) => {
3077 // Return an object that has two methods called `increment` and `decrement`.
3178 // `increment` should increment a counter variable in closure scope and return it.
3279 // `decrement` should decrement the counter variable and return it.
33- } ;
80+ let count = 0 ;
81+ function myIncrement ( ) {
82+ console . log ( "Here's the count: " + count ) ;
83+ return count ++ ;
84+ } ;
85+ function myDecrement ( ) {
86+ return count -- ;
87+ }
88+ return { increment : myIncrement , decrement : myDecrement } ;
89+ } ;
90+
91+ const myCounter3 = counterFactory ( ) ;
92+ console . log ( "myCounter3: " + myCounter3 . increment ( ) ) ;
93+ console . log ( "myCounter3: " + myCounter3 . increment ( ) ) ;
94+ console . log ( "myCounter3: " + myCounter3 . increment ( ) ) ;
95+ console . log ( "myCounter3: " + myCounter3 . decrement ( ) ) ;
96+ console . log ( "myCounter3: " + myCounter3 . decrement ( ) ) ;
97+ console . log ( "myCounter3: " + myCounter3 . decrement ( ) ) ;
0 commit comments