Skip to content

Commit 2ae178f

Browse files
js notes (#3)
1 parent 5dfc3e3 commit 2ae178f

File tree

11 files changed

+1562
-42
lines changed

11 files changed

+1562
-42
lines changed
File renamed without changes.

Data-Types/data-type.md

Lines changed: 644 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,23 @@ Welcome to your comprehensive JavaScript interview notes! This repository contai
55
---
66

77
## 📚 Table of Contents
8+
- [Data Types](Data-Types/data-type.md)
9+
- [loops](loops/loops.md)
10+
- [String](string/string.md)
11+
- [Arrays](Arrays/array.md)
12+
- [Deep Copy vs Shallow Copy](DeepCopyVSShallowCopy/shallow-deep-copy.md)
13+
- [Sets](map-set/set.md)
14+
- [JavaScript Regular Expression](string/regex.md)
815
- [Functions](functions/functions.md)
9-
- [Closures and Scope](Closures and Scope/closures.md)
16+
- [Callbacks](promises/callback.md)
17+
- [Closures and Scope](Closures-and-Scope/closures.md)
1018
- [Event Loop](JS-Interview-Questions/eventloop.md)
1119
- [Higher Order Functions](JS-Interview-Questions/HigherOrderFunction.md)
1220
- [Map, Reduce, Filter](JS-Interview-Questions/map-reduce-filters.md)
1321
- [Polyfills](HigherOrderFunction/polyfills.html)
1422
- [Promises](promises/promises.md)
1523
- [Async/Await](async-await/async-await.md)
24+
- [Debouncing and Throttling](debouncing_Throttling/debouncing_throttling.md)
1625
- [More Topics Coming Soon!]
1726

1827
---
@@ -36,15 +45,23 @@ Welcome to your comprehensive JavaScript interview notes! This repository contai
3645
---
3746

3847
## 📖 Index
48+
- [Data Types](Data-Types/data-type.md)
49+
- [loops](loops/loops.md)
50+
- [String](string/string.md)
3951
- [Arrays](Arrays/array.md)
52+
- [Deep Copy vs Shallow Copy](DeepCopyVSShallowCopy/shallow-deep-copy.md)
53+
- [Sets](map-set/set.md)
54+
- [JavaScript Regular Expression](string/regex.md)
4055
- [Functions](functions/functions.md)
41-
- [Closures](Closures/README.md)
42-
- [Event Loop](JS%20Interview%20Questions/eventloop.md)
56+
- [Callbacks](promises/callback.md)
57+
- [Closures and Scope](Closures-and-Scope/closures.md)
58+
- [Event Loop](JS-Interview-Questions/eventloop.md)
4359
- [Higher Order Functions](JS-Interview-Questions/HigherOrderFunction.md)
4460
- [Map, Reduce, Filter](JS-Interview-Questions/map-reduce-filters.md)
4561
- [Polyfills](HigherOrderFunction/polyfills.html)
4662
- [Promises](promises/promises.md)
4763
- [Async/Await](async-await/async-await.md)
64+
- [Debouncing and Throttling](debouncing_Throttling/debouncing_throttling.md)
4865
---
4966

5067

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
const input = document.querySelector("input");
2+
const debouncedValue = document.getElementById("debouncedValue");
3+
const inputValue = document.getElementById("value");
4+
5+
input.addEventListener("input", updateValue);
6+
7+
const handler = updateValueDebounced(updateValue1, 2000);
8+
input.addEventListener("input", handler);
9+
10+
function updateValue(e) {
11+
console.log("updating value");
12+
inputValue.textContent = e.target.value;
13+
}
14+
15+
function updateValue1(e) {
16+
debouncedValue.textContent = e.target.value;
17+
}
18+
19+
function updateValueDebounced(fn, delay) {
20+
let timeoutId;
21+
console.log(" debounced call");
22+
return function (...args) {
23+
clearTimeout(timeoutId);
24+
timeoutId = setTimeout(() => fn(...args), delay);
25+
};
26+
}
27+
28+
// debounce seacrh Practice
29+
30+
const searchInput = document.getElementById("searchInput");
31+
const results = document.getElementById("results");
32+
33+
searchInput.addEventListener("input", debounce(handleSearch, 1000));
34+
35+
function handleSearch(event) {
36+
const query = event.target.value;
37+
// Simulate an API call with a timeout
38+
console.log("Searching for:", query);
39+
setTimeout(() => {
40+
const fakeResults = [
41+
"apple",
42+
"banana",
43+
"orange",
44+
"grape",
45+
"watermelon",
46+
"kiwi",
47+
"mango",
48+
"mango smoothie",
49+
"strawberry",
50+
].filter((item) => item.includes(query.toLowerCase()));
51+
displayResults(fakeResults);
52+
}, 300);
53+
}
54+
55+
function displayResults(items) {
56+
results.innerHTML = "";
57+
items.forEach((item) => {
58+
const li = document.createElement("li");
59+
li.textContent = item;
60+
results.appendChild(li);
61+
});
62+
}
63+
64+
function debounce(fn, delay) {
65+
let timeoutId;
66+
console.log("+++++++++debounce call+++++++++");
67+
return function (...args) {
68+
clearTimeout(timeoutId);
69+
timeoutId = setTimeout(() => fn(...args), delay);
70+
};
71+
}
72+
73+
// Debounce keypress and keyup events
74+
const inputField = document.querySelector("input");
75+
76+
const debouncedKeyPressHandler = debounce(handleKeyPress, 500);
77+
const debouncedKeyUpHandler = debounce(handleKeyUp, 500);
78+
79+
inputField.addEventListener("keypress", debouncedKeyPressHandler);
80+
inputField.addEventListener("keyup", debouncedKeyUpHandler);
81+
82+
function handleKeyPress(event) {
83+
console.log("Key Pressed:", event.key);
84+
// You can add your logic here for keypress event
85+
}
86+
87+
function handleKeyUp(event) {
88+
console.log("Key Released:", event.key);
89+
// You can add your logic here for keyup event
90+
}
91+
92+
const throttle = (func, limit) => {
93+
let inThrottle;
94+
return function (...args) {
95+
if (!inThrottle) {
96+
func.apply(this, args);
97+
inThrottle = true;
98+
setTimeout(() => (inThrottle = false), limit);
99+
}
100+
};
101+
};
102+
103+
104+
const container = document.getElementById("throttle-container");
105+
// const handleScroll = function () {
106+
// console.log("scrolling...");
107+
// };
108+
109+
const handleScroll = throttle(() => {
110+
const scrollTop = window.scrollY;
111+
console.log("Scroll position:", scrollTop);
112+
113+
// Expensive calculations
114+
if (scrollTop > 500) {
115+
console.log("User scrolled past 500px");
116+
}
117+
}, 500);
118+
119+
container.addEventListener("scroll", handleScroll);

0 commit comments

Comments
 (0)