Apply a given function to each element of an array and return the transformed results.
Apply Transform Over Each Element in Array
You are given an array arr and a function fn. Create a new array where each element is the result of applying fn to the corresponding element of arr.
Return the transformed array without modifying the original input array.
This is a simple function-building exercise that tests array iteration and function invocation.
Input Format
arr: an array of valuesfn: a function that accepts one element fromarrand returns a transformed value
Output Format
- Return a new array
resultsuch thatresult[i] = fn(arr[i])
Constraints
- Preserve the order of elements
- Do not mutate the original array
- The transformation function is applied independently to each element
Example 1
Input
arr = [1,2,3], fn = x => x + 1
Output
[2,3,4]
Explanation
Each element is increased by 1, and the results are collected in a new array.
Example 2
Input
arr = ["a","bb"], fn = s => s.length
Output
[1,2]
Explanation
The function is applied to each string, producing its length.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.