Skip to content

Commit 8ad651c

Browse files
Daily 30 Days of JavaScript Challenge Complite
1 parent f11fc3e commit 8ad651c

21 files changed

Lines changed: 1505 additions & 1 deletion

Classes (Day 28 - 30)/Classes.md

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
# 🏫 JavaScript Classes – সম্পূর্ণ গাইড
2+
3+
## 📘 কি হলো Class?
4+
5+
JavaScript–এ **Class** হলো একটি **blueprint** বা template, যা দিয়ে আমরা objects তৈরি করি।
6+
Class–এর মধ্যে আমরা **properties (data)** এবং **methods (functions)** রাখতে পারি।
7+
8+
Class–এর সাহায্যে object-oriented programming (OOP) করা সহজ হয়।
9+
10+
---
11+
12+
## 💡 Syntax
13+
14+
```javascript
15+
class Person {
16+
constructor(name, age) {
17+
this.name = name; // property
18+
this.age = age; // property
19+
}
20+
21+
// method
22+
greet() {
23+
console.log(`Hello, my name is ${this.name}`);
24+
}
25+
}
26+
```
27+
28+
### 🧠 ব্যাখ্যা
29+
30+
- class Person { ... } → Person নামের class তৈরি।
31+
- constructor → object তৈরি করার সময় run হয়।
32+
- this.name এবং this.age → এই object–এর property।
33+
- greet() → method, যেটি object থেকে call করা যায়।
34+
35+
36+
### 🧮 Object তৈরি (Instantiation)
37+
```js
38+
const person1 = new Person("Alice", 25);
39+
console.log(person1.name); // Output: Alice
40+
person1.greet(); // Output: Hello, my name is Alice
41+
```
42+
- new Person(...) → নতুন object তৈরি করে।
43+
- person1.name → property access।
44+
- person1.greet() → method call।
45+
46+
47+
🔹 Inheritance (উত্তরাধিকার)
48+
একটি class অন্য class–কে extend করতে পারে।
49+
```js
50+
class Student extends Person {
51+
constructor(name, age, grade) {
52+
super(name, age); // parent class constructor call
53+
this.grade = grade;
54+
}
55+
56+
study() {
57+
console.log(`${this.name} is studying in grade ${this.grade}`);
58+
}
59+
}
60+
61+
const student1 = new Student("Bob", 20, 12);
62+
student1.greet(); // Hello, my name is Bob
63+
student1.study(); // Bob is studying in grade 12
64+
65+
```
66+
- extends → inheritance দেখায়।
67+
- super() → parent class constructor call করতে হয়।
68+
69+
70+
🔹 Static Methods
71+
Class–এর method শুধুমাত্র class থেকে call করা যায়, object থেকে নয়।
72+
```js
73+
class MathUtil {
74+
static square(x) {
75+
return x * x;
76+
}
77+
}
78+
79+
console.log(MathUtil.square(5)); // 25
80+
81+
```
82+
static keyword → object নয়, class–এর সাথে directly যুক্ত।
83+
84+
85+
🔹 Getters & Setters
86+
Class–এ property access করার জন্য getter ও setter ব্যবহার করা যায়।
87+
```js
88+
class Rectangle {
89+
constructor(width, height) {
90+
this.width = width;
91+
this.height = height;
92+
}
93+
94+
get area() {
95+
return this.width * this.height;
96+
}
97+
98+
set setWidth(width) {
99+
this.width = width;
100+
}
101+
}
102+
103+
const rect = new Rectangle(5, 10);
104+
console.log(rect.area); // 50
105+
rect.setWidth = 7;
106+
console.log(rect.area); // 70
107+
108+
```
109+
110+
🔹 Private Fields (ES2022)
111+
```#``` দিয়ে private property তৈরি করা যায়, যা class বাহিরে access করা যায় না।
112+
113+
114+
115+
116+
117+
```js
118+
class BankAccount {
119+
#balance;
120+
121+
constructor(initialBalance) {
122+
this.#balance = initialBalance;
123+
}
124+
125+
deposit(amount) {
126+
this.#balance += amount;
127+
}
128+
129+
getBalance() {
130+
return this.#balance;
131+
}
132+
}
133+
134+
const acc = new BankAccount(100);
135+
acc.deposit(50);
136+
console.log(acc.getBalance()); // 150
137+
// console.log(acc.#balance); // Error
138+
139+
```
140+
141+
142+
143+
144+
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# 🧩 LeetCode 2694: Event Emitter
2+
3+
## 📘 সমস্যা বিবরণ (Problem Description)
4+
5+
তোমাকে একটি **Event Emitter** class তৈরি করতে হবে।
6+
Event Emitter হলো একটি design pattern, যা **event–triggered programming** কে সহজ করে।
7+
8+
- **on(eventName, callback)** → একটি callback function একটি event এর সাথে যুক্ত করে।
9+
- **emit(eventName, args)** → event trigger করলে, সব callback–গুলো run হয়।
10+
- **off(eventName, callback)** → event থেকে নির্দিষ্ট callback remove করা।
11+
12+
---
13+
14+
## 💡 Intuition
15+
16+
1. প্রতিটি event–এর জন্য একটি **callback list** রাখব।
17+
2. `on` → callback add করবে।
18+
3. `emit` → callback list traverse করে run করবে।
19+
4. `off` → callback list থেকে remove করবে।
20+
21+
---
22+
23+
## 🧮 JavaScript সমাধান
24+
25+
```javascript
26+
class EventEmitter {
27+
constructor() {
28+
// Map eventName -> list of callbacks
29+
this.events = new Map();
30+
}
31+
32+
/**
33+
* Add listener
34+
* @param {string} eventName
35+
* @param {Function} callback
36+
*/
37+
on(eventName, callback) {
38+
if (!this.events.has(eventName)) {
39+
this.events.set(eventName, []);
40+
}
41+
this.events.get(eventName).push(callback);
42+
}
43+
44+
/**
45+
* Remove listener
46+
* @param {string} eventName
47+
* @param {Function} callback
48+
*/
49+
off(eventName, callback) {
50+
if (!this.events.has(eventName)) return;
51+
const list = this.events.get(eventName);
52+
this.events.set(
53+
eventName,
54+
list.filter(cb => cb !== callback)
55+
);
56+
}
57+
58+
/**
59+
* Emit event
60+
* @param {string} eventName
61+
* @param {...any} args
62+
*/
63+
emit(eventName, ...args) {
64+
if (!this.events.has(eventName)) return;
65+
for (const cb of this.events.get(eventName)) {
66+
cb(...args);
67+
}
68+
}
69+
}
70+
```
71+
72+
### 🧠 ব্যাখ্যা
73+
| ধাপ | কাজ | উদাহরণ |
74+
| --- | -------------------------------------- | ------------------------- |
75+
| 1️⃣ | `constructor` → Map তৈরি | `this.events = new Map()` |
76+
| 2️⃣ | `on(event, cb)` → callback add | `"click"``[cb1, cb2]` |
77+
| 3️⃣ | `off(event, cb)` → callback remove | `"click"``[cb2]` |
78+
| 4️⃣ | `emit(event, args...)` → callback call | `cb2(args)` |
79+
80+
### 🧾 উদাহরণ
81+
```js
82+
const emitter = new EventEmitter();
83+
84+
function cb1(data) { console.log('cb1', data); }
85+
function cb2(data) { console.log('cb2', data); }
86+
87+
emitter.on("event1", cb1);
88+
emitter.on("event1", cb2);
89+
90+
emitter.emit("event1", 42);
91+
// Output:
92+
// cb1 42
93+
// cb2 42
94+
95+
emitter.off("event1", cb1);
96+
emitter.emit("event1", 100);
97+
// Output:
98+
// cb2 100
99+
100+
```
101+
102+
🔹 Key Points
103+
- Event Emitter হলো publish-subscribe pattern।
104+
- একই event multiple callback রাখতে পারা যায়।
105+
- emit করলে সব callback sequentially run হয়।
106+
- off দিয়ে callback remove করা যায়।
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class EventEmitter {
2+
constructor() {
3+
this.events = new Map();
4+
this.subscriptions = [];
5+
}
6+
7+
subscribe(eventName, callback) {
8+
if (!this.events.has(eventName)) {
9+
this.events.set(eventName, []);
10+
}
11+
12+
const callbacks = this.events.get(eventName);
13+
callbacks.push(callback);
14+
const index = this.subscriptions.length;
15+
this.subscriptions.push({ eventName, callback });
16+
17+
return {
18+
unsubscribe: () => {
19+
const sub = this.subscriptions[index];
20+
if (!sub) return;
21+
const arr = this.events.get(sub.eventName);
22+
if (arr) {
23+
const i = arr.indexOf(sub.callback);
24+
if (i !== -1) arr.splice(i, 1);
25+
}
26+
this.subscriptions[index] = null;
27+
}
28+
};
29+
}
30+
31+
emit(eventName, args = []) {
32+
const callbacks = this.events.get(eventName) || [];
33+
const results = [];
34+
for (const cb of callbacks) {
35+
results.push(cb(...args));
36+
}
37+
return results;
38+
}
39+
}
40+
41+
/**
42+
* const emitter = new EventEmitter();
43+
*
44+
* // Subscribe to the onClick event with onClickCallback
45+
* function onClickCallback() { return 99 }
46+
* const sub = emitter.subscribe('onClick', onClickCallback);
47+
*
48+
* emitter.emit('onClick'); // [99]
49+
* sub.unsubscribe(); // undefined
50+
* emitter.emit('onClick'); // []
51+
*/
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# 🧩 LeetCode 2695: Array Wrapper
2+
3+
## 📘 সমস্যা বিবরণ (Problem Description)
4+
5+
তোমাকে একটি **Array Wrapper class** তৈরি করতে হবে যা একটি array কে wrap করে এবং কিছু সুবিধা দেয়:
6+
7+
- **constructor(nums)** → array initialize করা।
8+
- **valueOf()** → object কে number হিসেবে convert করা যায়।
9+
- **toString()** → object কে string হিসেবে convert করা যায়।
10+
11+
এছাড়া, array–এর element–এর সাথে operator ব্যবহার করলে wrapper class ঠিকভাবে behave করবে।
12+
13+
---
14+
15+
## 💡 Intuition
16+
17+
1. Wrapper class–এ array store করা।
18+
2. `valueOf()` → array–এর sum return করবে, যাতে number operations ঠিক হয়।
19+
3. `toString()` → array–এর string representation return করবে।
20+
21+
---
22+
23+
## 🧮 JavaScript সমাধান
24+
25+
```javascript
26+
class ArrayWrapper {
27+
constructor(nums) {
28+
this.nums = nums; // array store
29+
}
30+
31+
// Number context (like +, -)
32+
valueOf() {
33+
return this.nums.reduce((a, b) => a + b, 0);
34+
}
35+
36+
// String context
37+
toString() {
38+
return `[${this.nums.join(',')}]`;
39+
}
40+
}
41+
```
42+
43+
### 🧠 ব্যাখ্যা
44+
45+
| ধাপ | কাজ | উদাহরণ |
46+
| --- | ----------------------------------- | ----------------------------- |
47+
| 1️⃣ | `constructor(nums)` → nums save করা | `[1,2,3]` |
48+
| 2️⃣ | `valueOf()` → number conversion | `+wrapper` → 6 |
49+
| 3️⃣ | `toString()` → string conversion | `String(wrapper)` → "[1,2,3]" |
50+
51+
### 🧾 উদাহরণ
52+
```js
53+
const wrap1 = new ArrayWrapper([1,2,3]);
54+
const wrap2 = new ArrayWrapper([4,5,6]);
55+
56+
console.log(+wrap1); // 6
57+
console.log(`${wrap1}`); // "[1,2,3]"
58+
console.log(wrap1 + wrap2); // 6 + 15 = 21
59+
60+
console.log(wrap1.toString()); // "[1,2,3]"
61+
console.log(wrap2.valueOf()); // 15
62+
63+
```
64+
65+
🔹 Key Points
66+
- valueOf() → number context handle করে।
67+
- toString() → string context handle করে।
68+
- JavaScript–এ object operator use করলে valueOf() automatically call হয়।
69+
- Wrapper class দিয়ে array–কে context–specific behavior দেওয়া যায়।
70+
71+

0 commit comments

Comments
 (0)