-
Notifications
You must be signed in to change notification settings - Fork 0
/
dotProduct2.js
45 lines (30 loc) · 1.28 KB
/
dotProduct2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Function dotproduct2()
// Authors: Corey Devin Anderson and Kirankumar Batchu
//------------------------------------------------------------------------------
// Description:
// Function for calculating the dot product of two arrays. Similar to math.dot
// but can account for null components.
//------------------------------------------------------------------------------
// Parameters:
// a, b : 1D arrays of equal length for which the dot product will be calculated
// Returns: a scalar representing the dot product of a and b.
// Dependencies: none.
//------------------------------------------------------------------------------
// Version that does not handle null values.
// function dotProduct(a, b) {
// let dotSum = 0;
// let product = a.map((x, i) => (x * b[i]));
// product.forEach(element => dotSum = dotSum + element)
// return dotSum;
// }
// console.log(dotProduct(FIPS_01001, FIPS_01003))
// console.log(math.dot(FIPS_01001, FIPS_01003))
// START
function dotProduct2(a, b) {
let dotSum = 0;
let product = a.map(function(x, i){if (x != null && b[i] != null) {return x * b[i]} else {return null}});
product.forEach(element => dotSum = dotSum + element)
return dotSum;
}
// END
//------------------------------------------------------------------------------