Files
freeCodeCamp/curriculum/challenges/chinese/22-rosetta-code/rosetta-code-challenges/dot-product.md
2024-01-24 19:52:36 +01:00

1.4 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
5a23c84252665b21eecc7e1e 点乘法 1 302251 dot-product

--description--

Create a function, to compute the dot product, also known as the scalar product of two vectors.

--hints--

dotProduct 应该是一个函数。

assert(typeof dotProduct == 'function');

dotProduct([1, 3, -5], [4, -2, -1]) 应该返回一个数字。

assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number');

dotProduct([1, 3, -5], [4, -2, -1]) 应该返回 3

assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3);

dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]) 应该返回 130

assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130);

dotProduct([5, 4, 3, 2], [7, 8, 9, 6]) 应该返回 106

assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106);

dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]) 应该返回 -36

assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36);

dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]) 应该返回 10392

assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392);

--seed--

--seed-contents--

function dotProduct(ary1, ary2) {

}

--solutions--

function dotProduct(ary1, ary2) {
  var dotprod = 0;
  for (var i = 0; i < ary1.length; i++) dotprod += ary1[i] * ary2[i];
  return dotprod;
}