{ "data": { "question": { "questionId": "2747", "questionFrontendId": "2635", "boundTopicId": null, "title": "Apply Transform Over Each Element in Array", "titleSlug": "apply-transform-over-each-element-in-array", "content": "
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.
\n
Example 1:
\n\n\nInput: arr = [1,2,3], fn = function plusone(n) { return n + 1; }\nOutput: [2,3,4]\nExplanation:\nconst newArray = map(arr, plusone); // [2,3,4]\nThe function increases each value in the array by one. \n\n\n
Example 2:
\n\n\nInput: arr = [1,2,3], fn = function plusI(n, i) { return n + i; }\nOutput: [1,3,5]\nExplanation: The function increases each value by the index it resides in.\n\n\n
Example 3:
\n\n\nInput: arr = [10,20,30], fn = function constant() { return 42; }\nOutput: [42,42,42]\nExplanation: The function always returns 42.\n\n\n
\n
Constraints:
\n\n0 <= arr.length <= 1000
-109 <= arr[i] <= 109
fn returns a number
Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES6 features.
lodash.js library is included by default.
\\r\\n\\r\\nFor Priority Queue / Queue data structures, you may use 5.3.0 version of datastructures-js/priority-queue and 4.2.1 version of datastructures-js/queue.
\"], \"typescript\": [\"Typescript\", \"TypeScript 4.5.4, Node.js 16.13.2
.
Your code is run with --harmony
flag, enabling new ES2020 features.
lodash.js library is included by default.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }