
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Accumulating array elements to form new array in JavaScript
Problem
We are required to write a JavaScript function that takes in an array of numbers, arr, as the first argument and a number, num, (num <= length of array) as the second argument
Our function should add up each contiguous subarray of length num of the array arr to form corresponding elements of new array and finally return that new array
For example, if the input to the function is −
const arr = [1, 2, 3, 4, 5, 6]; const num = 2;
Then the output should be−
const output = [3, 5, 7, 9, 11];
Output Explanation
Because 1 + 2 = 3, 2 + 3 = 5, and so on...
Example
Following is the code−
const arr = [1, 2, 3, 4, 5, 6]; const num = 2; const accumulateArray = (arr = [], num = 1) => { const res = []; let sum = 0, right = 0, left = 0; for(; right < num; right++){ sum += arr[right]; }; res.push(sum); while(right < arr.length){ sum -= arr[left]; sum += arr[right]; right++; left++; res.push(sum); }; return res; }; console.log(accumulateArray(arr, num));
Output
Following is the console output−
[3, 5, 7, 9, 11]
- Related Articles
- How to add new array elements at the beginning of an array in JavaScript?
- Counting the occurrences of JavaScript array elements and put in a new 2d array
- JavaScript - Constructs a new array whose elements are the difference between consecutive elements of the input array
- Formatting JavaScript Object to new Array
- How to replace elements in array with elements of another array in JavaScript?
- Mapping an array to a new array with default values in JavaScript
- C++ program to find array after inserting new elements where any two elements difference is in array
- Shift certain array elements to front of array - JavaScript
- Add two consecutive elements from the original array and display the result in a new array with JavaScript
- Rearranging array elements in JavaScript
- Can form target array from source array JavaScript
- Split array entries to form object in JavaScript
- Create new array without impacting values from old array in JavaScript?
- Compare array elements to equality - JavaScript
- Copying the elements of ArrayList to a new array in C#

Advertisements