0

I have two different arrays. I want sum of arrays by multiplying with each element with another array. For example:

function get_total() {
    var i = 0;
    var arr = [];
    var gift_val_len = $('.gift_value').length;
    $('.gift_value').each(function() {
        arr[i++]= parseFloat($(this).val());  // Or this.innerHTML, this.innerText
    });
    var j = 0;
    var arr1 = [];
    $('.gift_number').each(function() {
        arr1[j++]= parseFloat($(this).val());  // Or this.innerHTML, this.innerText
    });
}

Now there are two different arrays like arr1=[15, 20, 25], arr2=[1, 3, 5] and I want result like ((15*1) + (20*3) + (25*5)). How can we achieve this?

2
  • 1
    Do arrays have the same length always ? Commented May 18, 2016 at 12:21
  • yes.. both array will have same length always.. Commented May 18, 2016 at 12:23

4 Answers 4

2

You can do this with a single line with pure JavaScript (no jQuery), using the built-in .map() method:

var arr1=[15, 20, 25], arr2=[1, 3, 5];
var sum = 0;
var arr3 = arr1.map(function(x, index) {
  var curMult = x * arr2[index];
  sum += curMult;
  return curMult;
});
console.log(arr1);
console.log(arr2);
console.log(arr3);
console.log(sum);

This will also sum the elements, as mentioned in the question.

Note: the arrays must be of the same length, otherwise the code will crash.

Sign up to request clarification or add additional context in comments.

4 Comments

The question says sum, so maybe add .reduce(function(a, b) { return a+b;}, 0); ?
What I was just going to write. You can make it a bit more defensive by: arr1.map(function(x, i) { return x * arr2[i] || x * 1; }); If there is any chance the array might not have the same number of elements.
@Chintan thanks,but since it's already iterating the array, no need for another loop. Edited the answer to add sum.
@Deshiknaves valid concern, but IMO it's OK to demand arrays of same length. Many built-in methods also throw errors or crash with invalid input.
1

Try This:

  • Inside get_total function, dispatch the function mulitply below with arr1,arr2 params.

  • Or assign it to a variable, like var mult = multiply(arr1,arr2);

function multiply(arr1, arr2) {
  var res = [];
  for (var i = 0; i < arr1.length; i++) {
    res.push(arr1[i] * arr2[i]);
  };
  return res
};

Comments

0

To reduce an array to a single value you can use Array Reduce

// Code goes here

var arr1=[15, 20, 25], arr2=[1, 3, 5];


var result = arr1.reduce(function(previousValue, currentValue, currentIndex) {
  var computedValue = currentValue * arr2[currentIndex];
  return previousValue + computedValue;
});
document.write(result);
console.log(result);

Comments

0

Array.protototype.reduce() might be a good way to handle this.

function multiplyArrays (array1, array2) {
    return array1.reduce(function(prev, curr, i) {
      return prev + (curr * array2[i]);
    }, 0);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.