(nums, index)
| 6 | * @return {number[]} |
| 7 | */ |
| 8 | var createTargetArray = function(nums, index) { |
| 9 | let target = []; // initialize an empty array |
| 10 | for(let i=0; i<nums.length; i++){ // loop through the every element of an array |
| 11 | target.splice(index[i], 0, nums[i]); // add element to the target as per index |
| 12 | } |
| 13 | return target; // return the target array |
| 14 | }; |
| 15 | |
| 16 | console.log(createTargetArray([0,1,2,3,4],[0,1,2,2,1])); // [0,4,1,3,2] |
| 17 |