Mario
Mario

Reputation:

calculate the total and display

I have array with decimal value 1.19, 1.44, 4.59 and so-on

How to calculate sum and alert.

Upvotes: 0

Views: 350

Answers (3)

Himadri
Himadri

Reputation: 8876

Try this:

 function GetSum()
   { 
   var mynumbers = new Array(1.19, 1.44, 4.59,5.67);

    var sum = 0;
for (i=0; i<=mynumbers.length-1; i++)
  {

  sum = sum + parseFloat(mynumbers[i]);
  }
  alert(sum);
  }

Upvotes: 0

Canavar
Canavar

Reputation: 48108

an alternative to Davide's answer with JQuery each :

<script>
var arr = [ 1.19, 1.44, 4.59 ];
var sum = 0;

jQuery.each(arr, function() {
  sum += this;
});

alert(sum);
</script>

Upvotes: 1

Davide Gualano
Davide Gualano

Reputation: 12993

You don't need jquery for this, plain javascript is okay:

var tot = 0.0;
for (i = 0; i < array.length; i++)
    tot += array[i];
alert(tot);

Upvotes: 5

Related Questions