Javascript: Multiply and Sum Two Arrays

Viewed 59228

I have two arrays of equal length, and I need to multiply the corresponding (by index) values in each, and sum the results.

For example

var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];

would result in 34 (4*2+3*3+4*3+5*1).

What's the simplest to read way to write this?

16 Answers
function mul (arr1, arr2) {
    var n_array = (arr1,arr2).map(x => x * x)
    return n_array
    }
var a = [1,2,3]
var b = [1,2,3]
console.log(mul(a,b))

A single-line solution using ES6 .reduce():

const sum_products = arr1.reduce((sum, val, i) => sum + (val * arr2[i]), 0)

Here are 3 functions that all accomplish the same thing. They are listed in order of increasingly modern JavaScript syntax.

The first function uses basic language features that have been around since the beginning. This will work on really old browsers such as IE6.

The second function uses features from ECMASScript 5 which was introduced around 2009, and will work on IE9+.

The third function uses ECMASScript 2015 arrow functions and will not work on any version of IE, not even IE 11 unless you are using some type of build-step in your code to transpile down to ES5 like Babel or Typescript

/**
 * Multiplies the corresponding values of two arrays and then sums the product.
 * Supported in all legacy browsers
 * @param {number[]} arr1 The first array
 * @param {number[]} arr2 The second array
 **/
function sumOfProductsECMAScript4(arr1, arr2) {
    var total = 0;
    for(var i = 0; i < arr1.length; i += 1) {
        total += arr1[i] * arr2[i];
    }
}

/**
 * Multiplies the corresponding values of two arrays and then sums the product.
 * Supported in all mainstream browsers except IE 8 and older.
 * @param {number[]} arr1 The first array
 * @param {number[]} arr2 The second array
 **/
function sumOfProductsECMASScript5(arr1, arr2) {
    // The map function creates a new array of the product of arr1 and arr2 values
    // The reduce function then takes this array of products and adds up all the values
    return arr1
        .map(function (value, index) { return value * arr2[index]; })
        .reduce(function (sum, current) { return sum + current; }, 0);
}

/**
 * Multiplies the corresponding values of two arrays and then sums the product.
 * Supported in all mainstream browsers except IE.
 * @param {number[]} arr1 The first array
 * @param {number[]} arr2 The second array
 **/
function sumOfProductsECMASScript2015(arr1, arr2) {
    // The map function creates a new array of the product of arr1 and arr2 values
    // The reduce function then takes this array of products and adds up all the values
    return arr1
        .map((v, i) => v * arr2[i])
        .reduce((sum, v) => sum + v, 0);
}



// Define your arrays
let arr1 = [2,3,4,5];
let arr2 = [4,3,3,1];

// Usage
let answer = sumOfProductsECMASScript4(arr1, arr2);
let answer2 = sumOfProductsECMASScript5(arr1, arr2);
let answer3 = sumOfProductsECMASScript2015(arr1, arr2);

if you were doing much of this kind of thing, you would probably use a library like mathjs:

var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];
result=math.dot(arr1,arr2);

make it map and reduce

var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];

var result = arr1.map((v,i) => v * arr2[i]).reduce((x, y) => x + y, 0)

console.log(result)

Related