diff --git a/problem/implement-array-prototype-map_en.md b/problem/implement-array-prototype-map_en.md index f3e8ce82..083f48c9 100644 --- a/problem/implement-array-prototype-map_en.md +++ b/problem/implement-array-prototype-map_en.md @@ -1,4 +1,16 @@ +The function in this code takes two parameters - the callback function and `this` argument. We assign `this` to a new variable called array. +We use forEach to loop over the calling array because forEach will skip the empty elements. We call the callback function using the `.call` method +so that we can pass the this argument. We assign the output of the function to the index in the output so that the output has the corresponding +empty elements too. For example, if `this` has empty elements at index 2 and 4, the output too will have empty elements at the same indices. -There is no solution yet. +```javascript + Array.prototype.myMap = function(callbackFn, thisArg) { + const array = this; + const output = []; -Would you like to [contribute to the solution](https://github.com/BFEdev/BFE.dev-solutions/blob/main/problem/implement-array-prototype-map_en.md)? [Contribute guideline](https://github.com/BFEdev/BFE.dev-solutions#how-to-contribute) + this.forEach((element, index) => { + output[index] = callbackFn.call(thisArg, element, index, array); + }); + return output; +} +``` \ No newline at end of file