Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
191
rated 0 times [  193] [ 2]  / answers: 1 / hits: 64066  / 13 Years ago, thu, july 28, 2011, 12:00:00

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?


More From » arrays

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

[#90939] Wednesday, July 27, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jenna

Total Points: 706
Total Questions: 107
Total Answers: 106

Location: Azerbaijan
Member since Tue, Sep 21, 2021
3 Years ago
;