Search code examples
jqueryarraysmultiplication

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


Solution

  • 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)