![]() |
This article will demonstrate how to create an array containing a cumulative sum at each index from an array of integers in JavaScript. The Cumulative Sum is defined as the partial sum total for the given sequence and all its previous values at a certain index. It is also known as the running sum as it keeps on adding the sum of all previous values. For instance, consider the below array: Arr = [ 2, 7, 9, 4, 3 ] The Cumulative sum at every index will be: 0 : 2 Hence, the resulting array will be: Cumulative Sum = [ 2, 9, 18, 22, 25 ] Methods to Create Cumulative Sum ArrayIt can be done with the following methods: Table of Content Method 1: Using JavaScript LoopsLooping in JavaScript is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. Syntax:while(condition) { Example: In this example, we will demonstrate how to create an array having a cumulative sum using JavaScript for loop.
Output [ 2, 9, 18, 22, 25 ] Method 2: Using JavaScript array.map() MethodThe Javascript map() method in JavaScript creates an array by calling a specific function on each element present in the parent array. It is used to iterate and perform iterations over an array. Syntax:map((element) => { /* … */ }) Example: In this example, we will see how to create an array having a cumulative sum using JavaScript array.map() map.
Output [ 1, 4, 9, 16, 25, 36 ] Method 3: Using JavaScript array.forEach() MethodThe array.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array. Syntax:array.forEach( callback( element, index, arr ), thisValue ) Example: In this example, we will create Cumulative Sum Array using JavaScript array.map() method.
Output [ 1, 4, 9, 16, 25, 36 ] Method 4: Using JavaScript array.reduce() MethodThe Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator. Syntax:array.reduce( function(total, currentValue, currentIndex, arr), initialValue ); Example: This example will demonstrate how we can create an array having a cumulative sum using JavaScript array.reduce() method.
Output [ 1, 4, 9, 16, 25, 36 ] Method 5: Using a Generator Function:A generator function can create an array of cumulative sums by iterating through the input array and yielding the cumulative sum at each step. By using the spread operator (…) with the generator function, you can generate the cumulative sum array efficiently. Example: In this example we defines a generator function cumulativeSumGenerator that yields cumulative sums of an array. It iterates through array, updates the sum, and yields it. The result is converted to an array using the spread operator.
Output [ 1, 3, 6, 10, 15 ] Method 6: Using Array reduce() and Spread OperatorThis approach leverages the reduce() method in combination with the spread operator (…) to compute the cumulative sum array efficiently. Eaxmple:
Output [ 2, 9, 18, 22, 25 ] |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |