Skip to content

Commit dbc4c0d

Browse files
committed
Merge branch 'master' of https://github.com/mzb2599/Javascript
2 parents 21ce68d + c91740c commit dbc4c0d

File tree

8 files changed

+124
-2
lines changed

8 files changed

+124
-2
lines changed

closure.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ var w = 1;
44

55
function fna(){
66
var v = 5;
7-
v = 2;
7+
v = 2;
8+
console.log("in fna: ", v);
89
}
910

1011
function fnb(){
1112
console.log("in fnb: ", v);
12-
console.log("in fnb: ", v);
1313
}
1414

1515
console.log("Outside ",v);

debouncing/debounce.html

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<input type="text" id="debounceInput" placeholder="Type something..." />
10+
<script>
11+
function debounceInput(func, delay) {
12+
let timeout;
13+
return function (...args) {
14+
const context = this;
15+
clearTimeout(timeout);
16+
timeout = setTimeout(() => func.apply(context, args), delay);
17+
};
18+
}
19+
20+
function fetchSearchResults(query) {
21+
console.log(`Fetching results for: ${query}`);
22+
// Simulate an API call
23+
// In a real application, you would make an AJAX request here
24+
// For example: take jsonplaceholder.typicode.com as a mock API
25+
fetch(`https://jsonplaceholder.typicode.com/posts?search=${query}`)
26+
.then((response) => response.json())
27+
.then((data) => console.log(data))
28+
.catch((error) => console.error("Error fetching data:", error));
29+
}
30+
31+
const searchInput = document.getElementById("debounceInput");
32+
33+
const debouncedSearch = debounceInput(function () {
34+
fetchSearchResults(searchInput.value);
35+
}, 300);
36+
37+
searchInput.addEventListener("input", debouncedSearch);
38+
</script>
39+
</body>
40+
</html>

freecodecamp/arrowFunction.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
Rewrite the function assigned to the variable magic which returns a new Date() to use arrow function syntax.
3+
Also, make sure nothing is defined using the keyword var.
4+
*/
5+
6+
/*var magic = function() {
7+
return new Date();
8+
};*/
9+
10+
const magic = () => {
11+
return new Date();
12+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//Rewrite the myConcat function which appends contents of arr2 to arr1 so that the function uses arrow function syntax.
2+
3+
// var myConcat = function(arr1, arr2) {
4+
// return arr1.concat(arr2);
5+
// };
6+
7+
const myConcat = (arr1, arr2) => {
8+
return arr1.concat(arr2);
9+
};
10+
11+
console.log(myConcat([1, 2], [3, 4, 5]));

freecodecamp/defaultParameters.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//Set Default Parameters for Your Functions
2+
//Modify the function increment by adding default parameters so that it will add 1 to number if value is not specified.
3+
4+
// Only change code below this line
5+
const increment = (number, value = 1) => number + value;
6+
// Only change code above this line

freecodecamp/freezeObject.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
function freezeObj() {
3+
const MATH_CONSTANTS = {
4+
PI: 3.14,
5+
};
6+
// Only change code below this line
7+
Object.freeze(MATH_CONSTANTS);
8+
9+
// Only change code above this line
10+
try {
11+
//Below line will not affect the code as the object is freezed
12+
MATH_CONSTANTS.PI = 99;
13+
} catch (ex) {
14+
console.log(ex);
15+
}
16+
return MATH_CONSTANTS.PI;
17+
}
18+
19+
const PI = freezeObj();
20+
console.log(PI);

freecodecamp/mutateArray.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//An array is declared as const s = [5, 7, 2]. Change the array to [2, 5, 7] using various element assignments.
2+
const s = [5, 7, 2];
3+
function editInPlace() {
4+
// Only change code below this line
5+
6+
// Using s = [2, 5, 7] would be invalid
7+
var l = s.length;
8+
var t = s[l - 1];
9+
for (var i = 0; i < l; i++) {
10+
s[l - i - 1] = s[l - i - 2];
11+
}
12+
s[0] = t;
13+
// Only change code above this line
14+
}
15+
editInPlace();
16+
console.log(s);

freecodecamp/scope.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//Block scope and function scope
2+
function checkScope() {
3+
let i = "function scope";
4+
if (true) {
5+
let i = "block scope";
6+
console.log("Block scope i is: ", i);
7+
}
8+
console.log("Function scope i is: ", i);
9+
return i;
10+
}
11+
checkScope();
12+
13+
/*Output
14+
D:\Code\js\Javascript\freecodecamp>node scope.js
15+
Block scope i is: block scope
16+
Function scope i is: function scope
17+
*/

0 commit comments

Comments
 (0)