Skip to content

Practice Apply Transform Over Each Element in Array

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2635
LeetCode Problem

Apply Transform Over Each Element in Array

Problem Statement

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.

Source: Apply Transform Over Each Element in Array on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
arr = [1,2,3], fn = function plusone(n) { return n + 1; }
Output
[2,3,4]

# Example 2

Input
arr = [1,2,3], fn = function plusI(n, i) { return n + i; }
Output
[1,3,5]

# Example 3

Input
arr = [10,20,30], fn = function constant() { return 42; }
Output
[42,42,42]

Constraints

0 <= arr.length <= 1000
-10^9 <= arr[i] <= 10^9
fn returns an integer.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.