2021-06-15 00:49:18 -07:00
---
id: 594810f028c0303b75339ad3
2022-02-19 12:56:08 +05:30
title: Prodotto scalare
2021-06-15 00:49:18 -07:00
challengeType: 5
forumTopicId: 302343
dashedName: vector-dot-product
---
# --description--
2022-02-19 12:56:08 +05:30
Un vettore può avere uno o più valori rappresentati da una collezione ordinata. Esempi potrebbero essere (x), (x, y), o (x, y, z).
2021-06-15 00:49:18 -07:00
# --instructions--
2022-02-19 12:56:08 +05:30
Scrivi una funzione che prende due vettori (rappresentati come matrici unidimensionali) come input e calcola il loro prodotto scalare. La tua funzione dovrebbe restituire `null` per input non validi come vettori di diverse lunghezze, o per qualsiasi cosa che non sia due vettori.
2021-06-15 00:49:18 -07:00
# --hints--
2022-02-19 12:56:08 +05:30
`dotProduct` dovrebbe essere una funzione.
2021-06-15 00:49:18 -07:00
```js
assert.equal(typeof dotProduct, 'function');
```
2022-02-19 12:56:08 +05:30
`dotProduct()` dovrebbe restituire `null` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(dotProduct(), null);
```
2022-02-19 12:56:08 +05:30
`dotProduct([1], [1])` dovrebbe restituire `1` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(dotProduct([1], [1]), 1);
```
2022-02-19 12:56:08 +05:30
`dotProduct([1], [1, 2])` dovrebbe restituire `null` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(dotProduct([1], [1, 2]), null);
```
2022-02-19 12:56:08 +05:30
`dotProduct([1, 3, -5], [4, -2, -1])` dovrebbe restituire `3` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3);
```
2022-02-19 12:56:08 +05:30
`dotProduct([3, 2, 1], [2, 4, 2], [5, 3, 1])` dovrebbe restituire `null` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(dotProduct([3, 2, 1], [2, 4, 2], [5, 3, 1]), null);
```
2022-02-19 12:56:08 +05:30
`dotProduct([ 0, 3, 6, 9, 12 ], [ 0, 4, 8, 12, 16 ])` dovrebbe restituire `360` .
2021-06-15 00:49:18 -07:00
```js
assert.equal(dotProduct([ 0, 3, 6, 9, 12 ], [ 0, 4, 8, 12, 16 ]), 360);
```
# --seed--
## --seed-contents--
```js
function dotProduct(...vectors) {
}
```
# --solutions--
```js
function dotProduct(...vectors) {
if (!vectors || !vectors.length || vectors.length > 2 || vectors[0].length !== vectors[1].length) {
return null;
}
const vectorLen = vectors[0].length;
let prod = 0;
let sum = 0;
let j = vectorLen;
let i = 2;
// Sum terms
while (j--) {
i = 2;
prod = 1;
while (i--) {
prod *= vectors[i][j];
}
sum += prod;
}
return sum;
}
```