Created
October 9, 2018 09:06
-
-
Save Fraasi/9c338aa315a93f2a7371cc3db06c6244 to your computer and use it in GitHub Desktop.
Rewriting map function
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.myMap = function (callback, thisArg) { | |
if (!thisArg) thisArg = this | |
if (!Array.isArray(thisArg)) throw new Error('Not an Array') | |
if (typeof callback !== 'function') throw new Error('Callback must be a function') | |
const newArray = [] | |
for (let i = 0; i < thisArg.length; i++) { | |
newArray.push(callback(thisArg[i], i, thisArg)) | |
} | |
return newArray | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment