-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday15.js
219 lines (148 loc) · 5.05 KB
/
day15.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Day 15: Closures
// avtivity 1: Understading closures
// task 1: write a function that returns another function, where the inner function accesses a variable from the outer function's scope. call the inner function and log the result.
function outerFunction() {
let outerVariable = 'Hello from the outer function!';
function innerFunction() {
return outerVariable;
}
return innerFunction;
}
const myInnerFunction = outerFunction();
console.log(myInnerFunction());
// task 2: create a closures that maitains a pricate counter. implement functions to increment and get the current of the counter.
// function createCounter() {
// let count = 0;
// function increment() {
// count++;
// }
// function getCount() {
// return count;
// }
// return {
// increment: increment,
// getCount: getCount
// };
// }
// const myCounter = createCounter();
// myCounter.increment();
// myCounter.increment();
// console.log(myCounter.getCount());
// activity 2: practical closures
// task 3: write a function that generates unique Ids. use a closures to keep track of the last generated ID and increment it with each call.
// function createIdGenerator() {
// let lastId = 0;
// function generateId() {
// lastId++;
// return `ID-${lastId}`;
// }
// return generateId;
// }
// const idGenerator = createIdGenerator();
// console.log(idGenerator()); // Output: ID-1
// console.log(idGenerator()); // Output: ID-2
// console.log(idGenerator()); // Output: ID-3
// task 4: create a closure that captures a user's name returns a function that greets the user by name.
function createGreeter(userName) {
function greet() {
return `Hello, ${userName}!`;
}
return greet;
}
const greeterForAlice = createGreeter('Jayy Prajapati');
console.log(greeterForAlice());
// activity 3: closures in loops
// task 5: write a loop that creates an array of functions. each function should log its index when called. use a closures to ensure each function logs the correct index.
// const functionsArray = [];
// for (let i = 0; i < 5; i++) {
// functionsArray[i] = (function (index) {
// return function () {
// console.log(index);
// };
// })(i);
// }
// functionsArray.forEach(func => func());
// activity 4: module pattern
// task 6: use closures to create a simple module for managing a collection of items. implement methods to add, remove and list items.
// function createItemManager() {
// const items = [];
// function addItem(item) {
// items.push(item);
// }
// function removeItem(item) {
// const index = items.indexOf(item);
// if (index !== -1) {
// items.splice(index, 1);
// } else {
// console.log('Item not found.');
// }
// }
// function listItems() {
// console.log('Items in the collection:', items);
// }
// return {
// addItem: addItem,
// removeItem: removeItem,
// listItems: listItems
// };
// }
// const itemManager = createItemManager();
// itemManager.addItem('Apple');
// itemManager.addItem('Banana');
// itemManager.listItems();
// itemManager.removeItem('Apple');
// itemManager.listItems();
// itemManager.removeItem('Orange');
// itemManager.listItems();
// activity 5: Memoization
// task 7: write a function that memiizes the results of another function. use a closure to store the results of previous computatoions.
// function memoize(fn) {
// const cache = {};
// return function (...args) {
// // Convert arguments to a string key for the cache
// const key = JSON.stringify(args);
// if (cache[key]) {
// console.log('Fetching from cache:', key);
// return cache[key];
// }
// console.log('Computing result for:', key);
// const result = fn(...args);
// cache[key] = result;
// return result;
// };
// }
// function slowFunction(num) {
// let result = 0;
// for (let i = 0; i < 1e6; i++) {
// result += num;
// }
// return result;
// }
// const memoizedSlowFunction = memoize(slowFunction);
// console.log(memoizedSlowFunction(10)); // Computes and logs result
// console.log(memoizedSlowFunction(10)); // Fetches from cache
// console.log(memoizedSlowFunction(20)); // Computes and logs result
// console.log(memoizedSlowFunction(20)); // Fetches from cache
// task 8: create a memoized varsion of a function that calculates the factorial of a number.
function memoize(fn) {
const cache = {};
return function (n) {
if (cache[n] !== undefined) {
console.log('Fetching from cache:', n);
return cache[n];
}
console.log('Computing result for:', n);
const result = fn(n);
cache[n] = result;
return result;
};
}
function factorial(n) {
if (n === 0) return 1; // Base case
return n * factorial(n - 1); // Recursive case
}
const memoizedFactorial = memoize(factorial);
console.log(memoizedFactorial(5));
console.log(memoizedFactorial(5));
console.log(memoizedFactorial(6));
console.log(memoizedFactorial(6));