Monday, December 4, 2023
 Popular · Latest · Hot · Upcoming
57
rated 0 times [  58] [ 1]  / answers: 1 / hits: 7414  / 3 Years ago, fri, november 13, 2020, 12:00:00
const a = [1,2,3]
const b = [1,0,1]

const c = dotProduct(a,b) // will equal 4

What's an efficient way of implementing the dotProduct method without importing any new libraries?


More From » javascript

 Answers
1



dot = (a, b) => a.map((x, i) => a[i] * b[i]).reduce((m, n) => m + n);
console.log(dot([1,2,3], [1,0,1]));




Here we use Array.prototype.map to create a new array with multiplied results of each index and Array.prototype.reduce to sum the values of resulting array.


[#2310] Monday, November 9, 2020, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
victorr

Total Points: 193
Total Questions: 86
Total Answers: 105

Location: Pitcairn Islands
Member since Thu, Jun 24, 2021
3 Years ago
victorr questions
Sat, Jul 25, 20, 00:00, 3 Years ago
Thu, Jun 11, 20, 00:00, 4 Years ago
Mon, Aug 19, 19, 00:00, 4 Years ago
;