Akhil Gupta Akhil Gupta -4 years ago 94
jQuery Question

jQuery Array multiplication & addition

I have two arrays:

Ex:

count["1","3","1"..........]
,
amount["10","20","30"..........]


I need to perform multiplication & addition as:

var total = (1x10)+(2x20)+(1x30)...........


Can anyone help. Thanks

Answer Source

You can use this code

var count = [1, 3, 1];
var amount = [10, 20, 30];
var sum = 0;
for (var i = 0; i < count.length; i++) {
  sum += count[i] * amount[i];
}
console.log(sum)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download