CaffGeek
CaffGeek

Reputation: 22064

Javascript: Multiply and Sum Two Arrays

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?

Upvotes: 19

Views: 68852

Answers (16)

dodotdot
dodotdot

Reputation: 1

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)

Upvotes: 0

marzetti
marzetti

Reputation: 449

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

Upvotes: 0

TxRegex
TxRegex

Reputation: 2425

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

Upvotes: 1

Prateek patel
Prateek patel

Reputation: 31

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

Upvotes: 3

DJK
DJK

Reputation: 175

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

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

Upvotes: 2

Dang Cong Duong
Dang Cong Duong

Reputation: 506

var arr = [1,2,3,4];
var arr2 = [1,1,1,2];

You can use:

var squares = arr.concat(arr2).reduce((t,n)=>t+n);

or:

var squares = arr.map((a, i) => a + arr2[i]).reduce((t,n) => t+n);

console.log(squares);

Upvotes: 1

Vitim.us
Vitim.us

Reputation: 22148

Declare functions that does the operations you want.

var sum      = function(a, b){ return a + b; };
var subtract = function(a, b){ return a - b; };
var multiply = function(a, b){ return a * b; };
var divide   = function(a, b){ return a / b; };

Then you have a very readable way to perform any operation on two arrays like this:

var array1 = [1,2,3];
var array2 = [2,4,8];

operateArrays(array1, array2, sum);      //[3, 6, 11]
operateArrays(array1, array2, subtract); //[-1, -2, -5]
operateArrays(array1, array2, multiply); //[2, 8, 24]
operateArrays(array1, array2, divide);   //[0.5, 0.5, 0.375]

Using this function

/**
* Divide each number of an array of numbers by another array of numbers
* @param  {Array}    arrayA  The array of numbers
* @param  {Array}    arrayB  The array of numbers
* @param  {Function} fn      Function that performs an operation
* @return {Array}            The resulting array
* @author Victor N. www.vitim.us
*/
function operateArrays(arrayA, arrayB, fn){
    if(arrayA.length!==arrayB.length) throw new Error("Cannot operate arrays of different lengths");
    return arrayB.map(function(b, i){
        return fn(arrayA[i], b);
    });
}

Upvotes: 1

Buck
Buck

Reputation: 501

var arr1 = [2,3,4,5];
var arr2 = [4,3,3,1];
console.log(arr1.reduce(function(r,a,i){return r+a*arr2[i]},0));
34

This shows the "functional" approach rather than the "imperative" approach for calculating the dot product of two vectors. Functional approach (which tends to be more concise) is preferred in such a simple function implementation as requested by the OP.

Upvotes: 23

Suhail Ansari
Suhail Ansari

Reputation: 137

var a = [1,2,3,4,5];
var b = [5,4,3,2,1];

a.map(function(x, index){ //here x = a[index]
 return b[index] + x 
});

=>[6,6,6,6,6]

//if you want to add the elements of an array:

a.reduce(function(x, y){
 return x + y
});

=>15

You can read about Array.map here. and Array.reduce here

Upvotes: 7

Endophage
Endophage

Reputation: 21473

Other answers are almost certainly more efficient, but just to give you a recursive viewpoint (it's nicer in some other languages). It does assume the two arrays are of equal length as you didn't specify what to do if they're not.

function sumProducts(array1, array2) {
    if(array1.length) 
        return array1.pop() * array2.pop() + sumProducts(array1, array2);

    return 0;
}

Edit:

katspaugh suggested flipping the returns which is ever so slightly more efficient (don't have to ! the length).

Upvotes: 5

vandre
vandre

Reputation: 827

This seems pretty straight forward to me

var result=0;
for (var i=0; i<arr1.length;i++){
    result+=arr1[i]*arr2[i];   
}

Upvotes: 2

FishBasketGordo
FishBasketGordo

Reputation: 23142

My vote for simplest-to-read way to write this goes to the humble for loop:

var ii, sumOfProds = 0;
for (ii = 0; ii < arr1.length && ii < arr2.length; ii++) {
    sumOfProds += arr1[ii] * arr2[ii];
}

Upvotes: 3

Paul
Paul

Reputation: 141827

var i, result = 0;
for(i = 0; i < arr1.length; i++)
    result += arr1[i]*arr2[i];
alert(result);

Not that this will cause an error if arr2 is shorter than arr1, but you said they're equal length, so I didn't bother checking for it.

Upvotes: 3

Chris Baker
Chris Baker

Reputation: 50602

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


var result = 0;
for (var i=0; i < arr1.length; i++) {
  result += (arr1[i] * arr2[i]);
}

alert(result);

Try it here: http://jsfiddle.net/VQKPt/

Upvotes: 4

Mrchief
Mrchief

Reputation: 76218

Something like this:

var sum = 0;
for (var i=0, len = arr1.length; i < len; i++) {     // optimized looping
   sum += arr1[i] * arr2[i];
}

Upvotes: 2

Ryan Doherty
Ryan Doherty

Reputation: 38740

var sum = 0;
for(var i=0; i< arr1.length; i++) {
    sum += arr1[i]*arr2[i];
}

Upvotes: 14

Related Questions