Skip to content

Commit a9f96b9

Browse files
committed
Day 15
1 parent 9e3c05b commit a9f96b9

File tree

1 file changed

+167
-0
lines changed

1 file changed

+167
-0
lines changed

Day 15 closuser.js

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Activity 1: Understanding Closures
2+
3+
// 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.
4+
5+
function outerFunction() {
6+
let outerVariable = "I'm from outer function";
7+
8+
function innerFunction() {
9+
console.log(outerVariable);
10+
}
11+
12+
return innerFunction;
13+
}
14+
15+
const inner = outerFunction();
16+
inner(); // Logs: "I'm from outer function"
17+
18+
// Task 2:Create a closure that maintains a private counter. Implement functions to increment and get the current value of the counter.
19+
20+
function createCounter() {
21+
let counter = 0;
22+
23+
return {
24+
increment: function() {
25+
counter++;
26+
},
27+
getValue: function() {
28+
return counter;
29+
}
30+
};
31+
}
32+
33+
const counter = createCounter();
34+
counter.increment();
35+
counter.increment();
36+
console.log(counter.getValue()); // Logs: 2
37+
38+
//Activity 2: Practical Closures
39+
40+
// Task 3:Write a function that generates unique IDs. Use a closure to keep track of the last generated ID and increment it with each call.
41+
42+
function createIdGenerator() {
43+
let lastId = 0;
44+
45+
return function() {
46+
lastId++;
47+
return lastId;
48+
};
49+
}
50+
51+
const generateId = createIdGenerator();
52+
console.log(generateId()); // Logs: 1
53+
console.log(generateId()); // Logs: 2
54+
55+
//Task 4: Create a closure that captures a user’s name and returns a function that greets the user by name.
56+
57+
function createGreeter(name) {
58+
return function() {
59+
console.log(`Hello, ${name}!`);
60+
};
61+
}
62+
63+
const greetJohn = createGreeter("John");
64+
greetJohn(); // Logs: "Hello, John!"
65+
66+
67+
// Activity 3: Closures in Loops
68+
69+
// Task 5:Write a loop that creates an array of functions. Each function should log its index when called. Use a closure to ensure each function logs the correct index.
70+
71+
function createFunctions() {
72+
let functions = [];
73+
74+
for (let i = 0; i < 5; i++) {
75+
functions.push(function() {
76+
console.log(i);
77+
});
78+
}
79+
80+
return functions;
81+
}
82+
83+
const functions = createFunctions();
84+
functions[0](); // Logs: 0
85+
functions[1](); // Logs: 1
86+
functions[2](); // Logs: 2
87+
functions[3](); // Logs: 3
88+
functions[4](); // Logs: 4
89+
90+
91+
// Activity 4: Module Pattern
92+
93+
// Task 6:Use closures to create a simple module for managing a collection of items. Implement methods to add, remove, and list items.
94+
95+
const itemManager = (function() {
96+
let items = [];
97+
98+
return {
99+
addItem: function(item) {
100+
items.push(item);
101+
},
102+
removeItem: function(item) {
103+
const index = items.indexOf(item);
104+
if (index > -1) {
105+
items.splice(index, 1);
106+
}
107+
},
108+
listItems: function() {
109+
return items;
110+
}
111+
};
112+
})();
113+
114+
itemManager.addItem("item1");
115+
itemManager.addItem("item2");
116+
console.log(itemManager.listItems()); // Logs: ["item1", "item2"]
117+
itemManager.removeItem("item1");
118+
console.log(itemManager.listItems()); // Logs: ["item2"]
119+
120+
// Activity 5: Memoization
121+
122+
//Task 7:Write a function that memoizes the results of another function. Use a closure to store the results of previous computations.
123+
124+
function memoize(fn) {
125+
let cache = {};
126+
127+
return function(...args) {
128+
let key = JSON.stringify(args);
129+
if (!cache[key]) {
130+
cache[key] = fn(...args);
131+
}
132+
return cache[key];
133+
};
134+
}
135+
136+
function add(a, b) {
137+
return a + b;
138+
}
139+
140+
const memoizedAdd = memoize(add);
141+
console.log(memoizedAdd(1, 2)); // Logs: 3
142+
console.log(memoizedAdd(1, 2)); // Logs: 3 (from cache)
143+
144+
// Task 8:Create a memoized version of a function that calculates the factorial of a number.
145+
146+
function memoize(fn) {
147+
let cache = {};
148+
149+
return function(...args) {
150+
let key = JSON.stringify(args);
151+
if (!cache[key]) {
152+
cache[key] = fn(...args);
153+
}
154+
return cache[key];
155+
};
156+
}
157+
158+
function factorial(n) {
159+
if (n === 0) {
160+
return 1;
161+
}
162+
return n * factorial(n - 1);
163+
}
164+
165+
const memoizedFactorial = memoize(factorial);
166+
console.log(memoizedFactorial(5)); // Logs: 120
167+
console.log(memoizedFactorial(5)); // Logs: 120 (from cache)

0 commit comments

Comments
 (0)