2019-01-30 13:41:15 +05:30
---
id: 5a23c84252665b21eecc7e1e
title: Dot product
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302251
2019-01-30 13:41:15 +05:30
---
## Description
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
< section id = 'description' >
2019-07-18 17:32:12 +02:00
2019-01-30 13:41:15 +05:30
Create a function, to compute the < b > < a href = "https://en.wikipedia.org/wiki/Dot product" > dot product< / a > < / b > , also known as the < b > scalar product< / b > of two vectors.
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
< / section >
## Instructions
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
< section id = 'instructions' >
< / section >
## Tests
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
< section id = 'tests' >
2020-03-30 11:23:18 -05:00
```yml
2019-01-30 13:41:15 +05:30
tests:
- text: < code > dotProduct</ code > should be a function.
2020-03-30 11:23:18 -05:00
testString: assert(typeof dotProduct == 'function');
2019-01-30 13:41:15 +05:30
- text: < code > dotProduct([1, 3, -5], [4, -2, -1])</ code > should return a number.
2020-03-30 11:23:18 -05:00
testString: assert(typeof dotProduct([1, 3, -5], [4, -2, -1]) == 'number');
2019-01-30 13:41:15 +05:30
- text: < code > dotProduct([1, 3, -5], [4, -2, -1])</ code > should return < code > 3</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(dotProduct([1, 3, -5], [4, -2, -1]), 3);
2019-01-30 13:41:15 +05:30
- text: < code > dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10])</ code > should return < code > 130</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(dotProduct([1, 2, 3, 4, 5], [6, 7, 8, 9, 10]), 130);
2019-01-30 13:41:15 +05:30
- text: < code > dotProduct([5, 4, 3, 2], [7, 8, 9, 6])</ code > should return < code > 106</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(dotProduct([5, 4, 3, 2], [7, 8, 9, 6]), 106);
2019-01-30 13:41:15 +05:30
- text: < code > dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6])</ code > should return < code > -36</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(dotProduct([-5, 4, -3, 2], [-7, -8, 9, -6]), -36);
2019-01-30 13:41:15 +05:30
- text: < code > dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110])</ code > should return < code > 10392</ code > .
2020-03-30 11:23:18 -05:00
testString: assert.equal(dotProduct([17, 27, 34, 43, 15], [62, 73, 48, 95, 110]), 10392);
2019-01-30 13:41:15 +05:30
```
< / section >
## Challenge Seed
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
< section id = 'challengeSeed' >
2019-07-18 17:32:12 +02:00
2019-01-30 13:41:15 +05:30
< div id = 'js-seed' >
```js
2019-03-02 17:42:56 +09:00
function dotProduct(ary1, ary2) {
2020-09-15 09:57:40 -07:00
2019-01-30 13:41:15 +05:30
}
```
< / div >
< / section >
## Solution
2020-03-30 11:23:18 -05:00
2019-01-30 13:41:15 +05:30
< section id = 'solution' >
```js
2019-03-02 17:42:56 +09:00
function dotProduct(ary1, ary2) {
2020-03-30 11:23:18 -05:00
var dotprod = 0;
for (var i = 0; i < ary1.length ; i + + ) dotprod + = ary1 [ i ] * ary2 [ i ] ;
return dotprod;
2019-01-30 13:41:15 +05:30
}
```
2019-07-18 17:32:12 +02:00
< / section >