Skip to content

Commit 2eee83a

Browse files
map,filter reduce and polyfills
1 parent 480bd91 commit 2eee83a

File tree

7 files changed

+1281
-0
lines changed

7 files changed

+1281
-0
lines changed

HigherOrderFunction/filter.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Filter function
2+
// Filter function is basically used to filter the value inside an array. The arr.filter() method is used to create a new array from a given array consisting of only those elements from the given array which satisfy a condition set by the argument method.
3+
4+
const arr = [5, 1, 3, 2, 6];
5+
// filter odd values
6+
function isOdd(x) {
7+
return x % 2;
8+
}
9+
const oddArr = arr.filter(isOdd); // [5,1,3]
10+
console.log(oddArr); // Output: [5, 1, 3]
11+
// Other way of writing the above:
12+
const oddArrAlt = arr.filter((x) => x % 2);
13+
console.log(oddArrAlt); // Output: [5, 1, 3]
14+
15+
//pollyfill for filter
16+
Array.prototype.myFilter = function(callback) {
17+
let result = [];
18+
for (let i = 0; i < this.length; i++) {
19+
if (callback(this[i], i, this)) { // Pass current element, index, and array to the callback
20+
result.push(this[i]);
21+
}
22+
}
23+
return result;
24+
}
25+
function isEven(x){
26+
return x % 2 === 0 ;
27+
}
28+
const myEvenArr = arr.myFilter(isEven);
29+
console.log(myEvenArr); // Output: [ 2, 6 ]
30+
File renamed without changes.

HigherOrderFunction/map.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
2+
const arr = [1, 2, 3, 4, 5];
3+
4+
function double(x){
5+
return x * 2;
6+
}
7+
8+
function triple(x){
9+
return x * 3;
10+
}
11+
12+
const outputDouble = arr.map(double);
13+
console.log(outputDouble); // [2, 4, 6, 8, 10]
14+
// alernate way to use map
15+
16+
const outputDoubleAlt = arr.map(function(x) { // Using a regular function
17+
return x * 2;
18+
});
19+
// This is equivalent to the previous example
20+
// It uses a regular function instead of an arrow function
21+
22+
console.log(outputDoubleAlt); // [2, 4, 6, 8, 10]
23+
24+
const outputDoubleAltArrow = arr.map(x => x * 2);
25+
console.log(outputDoubleAltArrow); // [2, 4, 6, 8, 10]
26+
27+
const outputTriple = arr.map(triple);
28+
console.log(outputTriple); // [3, 6, 9, 12, 15]
29+
30+
function binary(x) {
31+
return x.toString(2);
32+
}
33+
const binaryArr = arr.map(binary);
34+
35+
// The above code can be rewritten as :
36+
const binaryArr1 = arr.map(function binary(x) {
37+
return x.toString(2);
38+
});
39+
40+
// OR -> Arrow function
41+
const binaryArr2 = arr.map((x) => x.toString(2));
42+
43+
const users = [
44+
{ firstName: "Aditya", lastName: "Kumar", age: 23 },
45+
{ firstName: "Ashish", lastName: "Kumar", age: 29 },
46+
{ firstName: "Ankit", lastName: "Roy", age: 29 },
47+
{ firstName: "Pranav", lastName: "Mukherjee", age: 50 },
48+
];
49+
const fullNameArray = users.map(x => {
50+
return x.firstName + " " + x.lastName;
51+
})
52+
console.log(fullNameArray);
53+
54+
// Polifill for map
55+
Array.prototype.myMap = function(callback) {
56+
let result = [];
57+
for (let i = 0; i < this.length; i++) {
58+
result.push(callback(this[i], i, this)); // Pass current element, index, and array to the callback
59+
}
60+
return result;
61+
};
62+
// This is a custom implementation of the map function that mimics the behavior of Array.prototype.map
63+
// It iterates over the array, applies the callback function to each element, and returns a new array with the results.
64+
// The callback function can take three arguments: the current element, its index, and the array
65+
// This allows for more flexibility in how the callback is used, similar to the built-in map
66+
// This custom implementation can be used in the same way as the built-in map function, allowing
67+
// developers to use it in their code without relying on the built-in method.
68+
// This is useful for environments where the built-in map function is not available or for educational purposes
69+
// Usage:
70+
console.log(arr.myMap(x => x * 200)); // [2, 4, 6]
71+
72+
73+

0 commit comments

Comments
 (0)