{ "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.

\n\n

The returned array should be created such that returnedArray[i] = fn(arr[i], i).

\n\n

Please solve it without the built-in Array.map method.

\n\n

 

\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\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 27, "dislikes": 1, "isLiked": null, "similarQuestions": "[{\"title\": \"Group By\", \"titleSlug\": \"group-by\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Filter Elements from Array\", \"titleSlug\": \"filter-elements-from-array\", \"difficulty\": \"Easy\", \"translatedTitle\": null}, {\"title\": \"Array Reduce Transformation\", \"titleSlug\": \"array-reduce-transformation\", \"difficulty\": \"Easy\", \"translatedTitle\": null}]", "exampleTestcases": "function plusone(n) { return n + 1; }\n[1,2,3]\nfunction plusI(n, i) { return n + i; }\n[1,2,3]\nfunction constant(n, i) { return 42; }\n[10,20,30]", "categoryTitle": "JavaScript", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "JavaScript", "langSlug": "javascript", "code": "/**\n * @param {number[]} arr\n * @param {Function} fn\n * @return {number[]}\n */\nvar map = function(arr, fn) {\n \n};", "__typename": "CodeSnippetNode" }, { "lang": "TypeScript", "langSlug": "typescript", "code": "function map(arr: number[], fn: (n: number, i: number) => number): number[] {\n\n};", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"1.2K\", \"totalSubmission\": \"1.3K\", \"totalAcceptedRaw\": 1183, \"totalSubmissionRaw\": 1290, \"acRate\": \"91.7%\"}", "hints": [ "Start by creating an array that will eventually be returned.", "Loop over each element in the passed array. Push fn(arr[i]) to the returned array." ], "solution": null, "status": null, "sampleTestCase": "function plusone(n) { return n + 1; }\n[1,2,3]", "metaData": "{\n \"name\": \"map\",\n \"params\": [\n {\n \"name\": \"arr\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"string\",\n \"name\": \"fn\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\n },\n \"manual\": true,\n \"languages\": [\n \"javascript\",\n \"typescript\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": false, "envInfo": "{\"javascript\": [\"JavaScript\", \"

Node.js 16.13.2.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES6 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\\r\\n\\r\\n

For 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.

\\r\\n\\r\\n

Your code is run with --harmony flag, enabling new ES2020 features.

\\r\\n\\r\\n

lodash.js library is included by default.

\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }