forked from jayesh2906/JavaScript-with-JC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvery-Polyfill.js
More file actions
30 lines (23 loc) · 861 Bytes
/
Copy pathEvery-Polyfill.js
File metadata and controls
30 lines (23 loc) · 861 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
👉Array.prototype.every and Its Polyfill
The every() method tests whether all elements in an array pass the
test implemented by the provided callback function.
💡Note - It does not mutate the original array, and returns a Boolean value.
👉 One Level Up :- We can create our own custom every( Polyfill of every ), Check out the code snippet below.👇
*/
const numbers = [1, 2, 3, 4, 5, 6];
const isGreaterThan5 = (value, index, array) => {
return value > 5;
};
const result = numbers.every(isGreaterThan5);
console.log("result", result); // false
Array.prototype.customEvery = function (callback) {
for (let i = 0; i < this.length; i++) {
if (!callback(this[i], i, this)) {
return false;
}
}
return true;
};
const resultCustom = numbers.customEvery(isGreaterThan5);
console.log("resultCustom", resultCustom); // false