Middle+
const customMap = (array, transform) => { // something like array.map(transform); }; const stringResults = customMap([1, 2, 3], item => item.toString()); const lengths = customMap(["hello", "world"], item => item.length);
sobes.tech AI
Answer from AI
The customMap function should accept an array and a transformation function, and return a new array where each element is the result of applying the function to the corresponding element of the original array. This is similar to the standard Array.prototype.map.
Example implementation:
const customMap = (array, transform) => {
const result = [];
for (let i = 0; i < array.length; i++) {
result.push(transform(array[i], i, array));
}
return result;
};
const stringResults = customMap([1, 2, 3], item => item.toString());
console.log(stringResults); // ["1", "2", "3"]
const lengths = customMap(["hello", "world"], item => item.length);
console.log(lengths); // [5, 5]
Here, we manually iterate over the array, apply the transform function to each element, and collect the results into a new array.