Created
August 3, 2022 18:44
-
-
Save shekhardtu/1c2ed226b191e512824f392f0e31d50d to your computer and use it in GitHub Desktop.
The forEach polyfils - Javascript Interview Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.prototype.customForEach = function(fn) { | |
const arr = this; | |
for(let i =0; i<arr.length; i++) { | |
fn(arr[i], i, arr) | |
} | |
}; | |
const array = [1,2,3,4,5]; | |
array.customForEach((item, i) => { | |
console.log(item*2) | |
}); | |
// output: 2 4 6 8 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment