## Calculate the sum of an array

I have created an array using the following JavaScript:

for (i = 0; i < numOfPeriodsCost; i++) { esclatedPrice *= (1 + annualElecIncrease) rateArray.push(esclatedPrice); annualCostA = consumptionA * rateArray[i] console.log("annualCostA= " + annualCostA); }

For context:

var numOfPeriodsCost = 10; var annualElecIncrease = 0.1; var consumptionA = 520;

The console logs the following for annualCostA:

How would I take this one step further and calculate the sum of the array?

Thanks

In your loop, you just need to add a variable `sum`

that will add the value of `annualCostA`

in each iteration like so:

var sum = 0; for (i = 0; i < numOfPeriodsCost; i++) { esclatedPrice *= (1 + annualElecIncrease) rateArray.push(esclatedPrice); annualCostA = consumptionA * rateArray[i] sum += annualCostA // add the annualCostA value in each iteration console.log("annualCostA= " + annualCostA); } console.log(sum); //total sum

**Program to find sum of elements in a given array,** Program to calculate sum of array in C - This program should give an insight of how to parse (read) array. We shall use a loop and sum up all values of the array. In order to find the sum of all elements in an array, we can simply iterate the array and add each element to a sum accumulating variable. This very simply starts with a sum of 0 and add each item in the array as we go: 2.2. Sum With the Java Stream API

You can use Array.prototype.reduce():

var sumOfRateArray = rateArray.reduce((a, c) => a + c, 0);

Code:

const rateArray = [12.100000000000001,13.310000000000002,14.641000000000004,16.105100000000004,17.715610000000005,19.487171000000007,21.43588810000001,23.579476910000015,25.937424601000018]; const sumOfRateArray = rateArray.reduce((a, c) => a + c, 0); console.log(sumOfRateArray);

**Java Program to find sum of array,** Recommended (reduce with default value). Array.prototype.reduce can be used to iterate through the array, adding the current element value to the sum of the Program to find sum of elements in a given array; Product of all Subarrays of an Array; Sliding Window Maximum : Set 2; Perfect Sum Problem; Minimize the maximum difference between adjacent elements in an array; Median of sliding window in an array; Eggs dropping puzzle | Set 2; Minimum number of swaps required to sort an array of first N number

Add a sum variable before the loop `let sum=0;`

Add the element you want to sum inside the loop `sum+= your_element;`

Print the result `console.log("Sum= "+sum);`

**Program to calculate sum of array in C,** In this tutorial we will see how to sum up all the elements of an array. @author: BeginnersBook.com * @description: Get sum of array elements how to find sum of first 5 elements of an array{1,2,3,4,5,6,7,8,9,10} in java using for-each? Create an array and define its values Define neediest variables with initialization for loop iterate to each value for getting the input valuef or loop iterate each value for calculating the variable Add each element of the sum variable from the array finally, display sum on the screen

Using Array.prototype.reduce() on your `rateArray`

variable after the loop:

var rateArray = [ 936, 1029.6, 1132.56 ]; var arraySum = rateArray.reduce((total, e) => total + e); console.log(arraySum)

**Sum of array elements - MATLAB sum,** In this quick tutorial, we'll cover how we can calculate sum & average in an array using both Java standard loops and the Stream API. 1.1.1 Calculate the sum of array elements Using for loop. 1.1.2 Calculate the sum of array elements – get input from the user. 1.1.3 Calculate the sum of array elements Using Advanced for loop. 1.1.4 Calculate the sum of array elements -takes input from the user.

var numOfPeriodsCost = 10; var annualElecIncrease = 0.1; var consumptionA = 520; let sum = 0; for (i = 0; i < numOfPeriodsCost; i++) { esclatedPrice *= (1 + annualElecIncrease); rateArray.push(esclatedPrice); annualCostA = consumptionA * rateArray[i] console.log("annualCostA= " + annualCostA); sum += annualCostA; } console.log('sum', sum);

**How to find the sum of an array of numbers,** C programming, exercises, solution: Write a program in C to find the sum of all elements of an array. JavaScript exercises, practice and solution: Write a JavaScript function to calculate the sum of values in an array.

**Java program to sum the elements of an array,** Sum = 5 + 2 + 7 + 9 + 6 = 29. Thus, the multitude of methods used to calculate the sum of all the elements of an array is as follows: Using Standard Method. Logic to find sum of array elements. Input size and elements in array , store in some variable say n and arr[n] . To store sum of array elements, initialize a variable sum = 0 . Note: sum must be initialized only with 0. To find sum of all elements, iterate through each element and add the current

**Find Sum and Average in a Java Array,** You can use the reduce() method to find or calculate the sum of an array of numbers. The reduce() method executes the specified reducer function on each member of the array resulting in a single output value, as demonstrated in the following example:

**C exercises: Find the sum of all elements of an array,** Find sum of all elements of array: -------------------------------------- Input the number of elements to be stored in the array :4 Input 4 elements in the array : element - 0 : 2