Open In App

How to Calculate the Cumulative Sum of Elements in an Array using JavaScript?

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn to calculate the cumulative sum of elements in an array using JavaScript. The cumulative sum of elements in an array is a new array where each element represents the sum of all preceding elements, including itself, in the original array.

There are several methods that can be used to calculate the cumulative sum of elements in an array using JavaScript, which is listed below:

  • Using the forEach loop approach
  • Using the map() method in JavaScript
  • Using a recursive function or a recursion approach

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 1: Using the forEach loop approach

In this specified approach, we will be using the forEach loop to go through the individual element of the input array, and the current element is added to the running sum variable, and this calculated sum is actually saved in the new array. This iteration continues till the last element is been calculated.

Syntax:

array.forEach(callback(element, index, arr), thisValue)

Example: In this example, we are using the forEach loop approach in JavaScript

Javascript




// Using forEach loop
let inputArray = [1, 2, 3, 4, 5, 6, 7];
let cummulativeSum = 0;
 
let outputCummulativeSumArray = [];
inputArray.forEach((element) => {
    cummulativeSum += element;
    outputCummulativeSumArray.push(cummulativeSum);
});
 
const outputSum =
    outputCummulativeSumArray[outputCummulativeSumArray.length - 1];
 
console.log("Cumulative Sum Array is:", outputCummulativeSumArray);
console.log("Total Cumulative Sum:", outputSum);


Output

Cumulative Sum Array is: [
   1,  3,  6, 10,
  15, 21, 28
]
Total Cumulative Sum: 28


Approach 2: Using the map() method in JavaScript

In this approach, we are using the inbuilt map() method which actually goes through every element of the array and stores the running sum, and also it returns the new array which consists of the cumulative sum at each index position.

Syntax:

map((element) => { /* … */ })

Example: In this example, we are using the map() method in JavaScript.

Javascript




//Using map() method
let inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
let cummulativeSum = 0;
 
let cumulativeSumArray = inputArray.map((element) => {
    cummulativeSum += element;
    return cummulativeSum;
});
 
let outputSum =
    cumulativeSumArray[cumulativeSumArray.length - 1];
 
console.log("Cumulative Sum Array is:", cumulativeSumArray);
console.log("Total Cumulative Sum:", outputSum);


Output

Cumulative Sum Array is: [
   1,  3,  6, 10, 15,
  21, 28, 36, 45, 55,
  66
]
Total Cumulative Sum: 66


Approach 3: Using recursive function

In this approach, we are using the recursion function to find the cumulative sum of array elements. The recursive function will go through the elements of the input given array, perform the add to the current element to the sum variable, and then store pr save each cumulative sum in the new array variable.

Syntax:

function functionName(/* parameters */) {
if (/* base case condition */) {
// return some value or perform an action
}
}

Example: In this example, we are using the recursive function in JavaScript.

Javascript




//Using Recursion
let inputArray = [1, 2, 3, 4];
let totalCummaltiveArray = [];
let cummulativeSum = 0;
function calculateCumulativeSumUsingRecursion(index) {
    if (index < inputArray.length) {
        cummulativeSum += inputArray[index];
        totalCummaltiveArray.push(cummulativeSum);
        calculateCumulativeSumUsingRecursion(index + 1);
    }
}
calculateCumulativeSumUsingRecursion(0);
let outputSum = totalCummaltiveArray[totalCummaltiveArray.length - 1];
console.log("Cumulative Sum Array is:", totalCummaltiveArray);
console.log("Total Cumulative Sum:", outputSum);


Output

Cumulative Sum Array is: [ 1, 3, 6, 10 ]
Total Cumulative Sum: 10




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads