2022-01-21 01:00:18 +05:30
|
|
|
---
|
|
|
|
id: 5a23c84252665b21eecc7e1e
|
2022-01-22 20:38:20 +05:30
|
|
|
title: ドット積
|
2022-01-21 01:00:18 +05:30
|
|
|
challengeType: 5
|
|
|
|
forumTopicId: 302251
|
|
|
|
dashedName: dot-product
|
|
|
|
---
|
|
|
|
|
|
|
|
# --description--
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
2つのベクトルの**スカラー積**としても知られる**[ドット積](https://en.wikipedia.org/wiki/Dot product)**を計算する関数を作成します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
# --hints--
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct` という関数です。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof dotProduct == 'function');
|
|
|
|
```
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct([1, 3, -5], [4, -2, -1])` は数字を返します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number');
|
|
|
|
```
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct([1, 3, -5], [4, -2, -1])` は `3` を返します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3);
|
|
|
|
```
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])` は `130` を返します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130);
|
|
|
|
```
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct([5, 4, 3, 2], [7, 8, 9, 6])` は `106` を返します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106);
|
|
|
|
```
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])` は `-36` を返します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36);
|
|
|
|
```
|
|
|
|
|
2022-01-22 20:38:20 +05:30
|
|
|
`dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])` は `10392` を返します。
|
2022-01-21 01:00:18 +05:30
|
|
|
|
|
|
|
```js
|
|
|
|
assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392);
|
|
|
|
```
|
|
|
|
|
|
|
|
# --seed--
|
|
|
|
|
|
|
|
## --seed-contents--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function dotProduct(ary1, ary2) {
|
|
|
|
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
# --solutions--
|
|
|
|
|
|
|
|
```js
|
|
|
|
function dotProduct(ary1, ary2) {
|
|
|
|
var dotprod = 0;
|
|
|
|
for (var i = 0; i < ary1.length; i++) dotprod += ary1[i] * ary2[i];
|
|
|
|
return dotprod;
|
|
|
|
}
|
|
|
|
```
|