Sum of array javascript for loop

I'm trying to find a way to sum all the numbers that have been added to an array. I believe this should work:

   var total = 0;

   for (var i  = 0; i < totalPrice.length; i++);

   total  += totalPrice[i];
   document.getElementById("displayPrice").innerHTML = total;

But total comes out as a NaN.

This is an example of my code in JSFiddle, if you add the item twice the value gets pushed into the array, what am I doing wrong with the for loop?

https://jsfiddle.net/bgv5s9re/2/

asked Oct 8, 2016 at 15:50

5

You could use brackets

  var total = 0;

   for (var i  = 0; i < totalPrice.length; i++){

      total  += totalPrice[i];

   }
   document.getElementById("displayPrice").innerHTML = total;

answered Oct 8, 2016 at 15:54

ScaisEdgeScaisEdge

130k10 gold badges89 silver badges100 bronze badges

2

Get the Sum of an Array of Numbers #

To get the sum of an array of numbers:

  1. Use the Array.reduce() method to iterate over the array.
  2. Set the initial value in the reduce method to 0.
  3. On each iteration, return the sum of the accumulated value and the current number.

Copied!

const arr = [5, 15, 45]; const sum = arr.reduce((accumulator, value) => { return accumulator + value; }, 0); console.log(sum); // 👉️ 65

The accumulator parameter is initially set to 0 because that's what we passed as the second argument to the reduce method.

On each iteration, we return the sum of the accumulator and the current array element.

An alternative and perhaps simpler approach is to use a for...of loop.

To get the sum of an array of numbers:

  1. Declare a sum variable and initialize it to 0.
  2. Use the for...of loop to iterate over the array.
  3. On each iteration, reassign the sum variable to its current value plus the value of the current element.

Copied!

const arr = [5, 15, 45]; let sum = 0; for (const value of arr) { sum += value; } console.log(sum); // 👉️ 65

The for...of loop allows us to iterate over an array.

Notice that we declared the sum variable using the let keyword. Had we declared the variable using const, we wouldn't be able to reassign it.

On each iteration, we reassign the sum variable to its current value plus the value of the current element.

You can also use a basic for loop to sum an array of numbers.

Copied!

const arr = [5, 15, 45]; let sum = 0; for (let index = 0; index < arr.length; index++) { sum += arr[index]; } console.log(sum); // 👉️ 65

This example achieves the same goal, but instead of using for...of we used a basic for loop.

How do you sum an array for a for loop?

Sum Array of Numbers with for loop.
Create an array of numbers, in the example int values..
Create a for statement, with an int variable from 0 up to the length of the array, incremented by one each time in the loop..
In the for statement add each of the array's elements to an int sum..

How do you find the sum of all elements in an array in JavaScript?

function sumArray(array) { const ourArray = [1, 4, 0, 9, -3]; let sum = 0; ​ for (let i = 0; i < ourArray. length; i += 1) { ... .
function sumArray(array) { let sum = 0; ​ array. forEach(item => { sum += item; ... .
function sumArray(array) { let sum = 0; ​ /*loop over array and add each item to sum. */ for (const item of array) {.

How do you sum a loop in JavaScript?

For loop sum in javascript.
let sum = 0;.
for (let i = 1; i <= 6; i++) {.
sum = sum + i;.
console. log(sum).
//Output: 21..

Is there a sum () in JavaScript?

sum() function in D3. js is used to return the sum of the given array's elements. If the array is empty then it returns 0.