forked from bloominstituteoftechnology/JavaScript-II
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosure.js
More file actions
120 lines (97 loc) · 2.91 KB
/
closure.js
File metadata and controls
120 lines (97 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// ==== Challenge 1: Write your own closure ====
// Write a simple closure of your own creation. Keep it simple!
const var1 = 'ABC';
function myFunction() {
const var2 = 'XYZ'
console.log(`My function sees var1 and its value of: ${var1}`);
return var2;
}
console.log(var1);
//the line below will return an error
//console.log(var2);
console.log(myFunction());
/* STRETCH PROBLEMS, Do not attempt until you have completed all previous tasks for today's project files */
// ==== Challenge 2: Create a counter function ====
const counter = () => {
// Return a function that when invoked increments and returns a counter variable.
let num = 0;
return function countingFunct () {
num++;
return num;
}
};
// Example usage: const newCounter = counter();
// newCounter(); // 1
// newCounter(); // 2
const newCounter = counter();
console.log(counter());
console.log(newCounter());
console.log(newCounter());
console.log(newCounter());
const thirdCounter = counter();
console.log(thirdCounter());
// ==== Challenge 3: Create a counter function with an object that can increment and decrement ====
const counterFactory = (initValue) => {
// Return an object that has two methods called `increment` and `decrement`.
// `increment` should increment a counter variable in closure scope and return it.
// `decrement` should decrement the counter variable and return it.
let count = initValue
return {
"increment": function() {
count++;
return count;
},
"decrement": function() {
count--;
return count;
}
}
};
const test = counterFactory(1000);
console.log(test);
console.log(test.increment());
console.log(test.increment());
console.log(test.increment());
console.log(test.decrement());
console.log(test.increment());
const testAgain = counterFactory(100);
console.log(testAgain.increment());
console.log(testAgain.increment());
console.log(test.increment());
// const countDown = counterFactory(100).decrement;
// console.log(countDown());
// console.log(countDown());
// console.log(countDown());
// console.log(countDown());
// console.log(countDown());
// const countUp = counterFactory(10).increment;
// console.log(countUp());
// console.log(countUp());
// console.log(countUp());
// console.log(countDown());
// console.log(countDown());
// console.log(countUp());
// ====== trying another way =====
const betterFactory = (initValue) => {
return {
"count": initValue,
"increment": function() {
this.count++;
return this.count;
},
"decrement": function() {
this.count--;
return this.count;
}
}
};
const bothWays = betterFactory(50);
console.log(bothWays);
console.log(bothWays.increment());
console.log(bothWays.increment());
console.log(bothWays.increment());
console.log(bothWays.increment());
console.log(bothWays.decrement());
console.log(bothWays.decrement());
console.log(bothWays.decrement());
console.log(bothWays.increment());