-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathclosures.js
More file actions
164 lines (122 loc) · 3.53 KB
/
closures.js
File metadata and controls
164 lines (122 loc) · 3.53 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Challenge 1
function createFunction() {
const innerFunc = () => {
console.log('hello');
}
return innerFunc;
}
// UNCOMMENT THESE TO TEST YOUR WORK!
// const function1 = createFunction();
// function1();
// Challenge 2
function createFunctionPrinter(input) {
const printerFunc = () => {
console.log(input);
}
return printerFunc;
}
// UNCOMMENT THESE TO TEST YOUR WORK!
// const printSample = createFunctionPrinter('sample');
// printSample();
// const printHello = createFunctionPrinter('hello');
// printHello();
// Challenge 3
function outer() {
let counter = 0; // this variable is outside incrementCounter's scope
function incrementCounter () {
counter ++;
console.log('counter', counter);
}
return incrementCounter;
}
const willCounter = outer();
const jasCounter = outer();
// Uncomment each of these lines one by one.
// Before your do, guess what will be logged from each function call.
// willCounter(); // Logs 'counter 1'
// willCounter(); // Logs 'counter 2'
// willCounter(); // Logs 'counter 3'
// jasCounter(); // Logs 'counter 1'
// willCounter(); // Logs 'counter 4'
// Challenge 4
function addByX(x) {
const addBy = num => {
return num + x;
}
return addBy;
}
const addByTwo = addByX(2);
// now call addByTwo with an input of 1
// console.log(addByTwo(1)); // Logs 3
// now call addByTwo with an input of 2
// console.log(addByTwo(2)); // Logs 4
//--------------------------------------------------
// Extension
//--------------------------------------------------
// Challenge 5
function once(func) {
let counter = 0;
let onceVal;
const innerFunc = val => {
if (counter === 0) onceVal = func(val);
counter++;
return onceVal;
}
return innerFunc;
}
const onceFunc = once(addByTwo);
// UNCOMMENT THESE TO TEST YOUR WORK!
// console.log(onceFunc(4)); //should log 6
// console.log(onceFunc(10)); //should log 6
// console.log(onceFunc(9001)); //should log 6
// Challenge 6
function after(count, func) {
let counter = 0;
return (val) => {
if (++counter >= count) func(val);
}
}
const called = function() { console.log('hello') };
const afterCalled = after(3, called);
// afterCalled(); // -> nothing is printed
// afterCalled(); // -> nothing is printed
// afterCalled(); // -> 'hello' is printed
// afterCalled(); // -> 'hello' is printed
// Challenge 7
function delay(func, wait, ...args) {
setTimeout(() => func(...args), wait);
}
// Challenge 8
function rollCall(names) {
return () => {
if (!names.length) return console.log('Everyone accounted for');
console.log(names.shift());
}
}
// UNCOMMENT THESE TO TEST YOUR WORK!
// const rollCaller = rollCall(['Victoria', 'Juan', 'Ruth'])
// rollCaller() // -> Should log 'Victoria'
// rollCaller() // -> Should log 'Juan'
// rollCaller() // -> Should log 'Ruth'
// rollCaller() // -> Should log 'Everyone accounted for'
function saveOutput(func, magicWord) {
let result = 0;
let output = {}
function wordChecker(input){
output = {...output, [input]: result}
if (input === magicWord){
return output
}
else {
result = func(input)
return result;
}
}
return wordChecker
}
// /*** Uncomment these to check your work! ***/
// const multiplyBy2 = function(num) { return num * 2; };
// const multBy2AndLog = saveOutput(multiplyBy2, 'boo');
// console.log(multBy2AndLog(2)); // => should log 4
// console.log(multBy2AndLog(9)); // => should log 18
// console.log(multBy2AndLog('boo')); // => should log { 2: 4, 9: 18 }