#2635
Easy JavaScript Apply transform over each element in array
86.2% acceptance
Mar 2, 2026
912
120
Given an integer array arr and a mapping function fn, return a new array with a transformation applied to each element.
The returned array should be created such that returnedArray[i] = fn(arr[i], i).
Please solve it without the built-in Array.map method.
Solution
TypeScript
Time O(n)
Space O(1)
function map(arr: number[], fn: (n: number, i: number) => number): number[] {
const result: number[] = new Array(arr.length);
for (let i = 0; i < arr.length; i++) {
result[i] = fn(arr[i], i);
}
return result;
}