2018-09-30 22:01:58 +00:00
|
|
|
---
|
|
|
|
title: Vector cross product
|
|
|
|
id: 594810f028c0303b75339ad2
|
|
|
|
challengeType: 5
|
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
|
|
|
<section id='description'>
|
|
|
|
A vector is defined as having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z).
|
|
|
|
<p>
|
|
|
|
Task:
|
|
|
|
|
|
|
|
Write a function that takes two vectors (arrays) as input and computes their cross product.
|
|
|
|
|
|
|
|
Your function should return <code>null</code> on
|
|
|
|
invalid inputs (ie vectors of different lengths).
|
|
|
|
</p>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Instructions
|
|
|
|
<section id='instructions'>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
2018-10-04 13:37:37 +00:00
|
|
|
tests:
|
|
|
|
- text: dotProduct must be a function
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(typeof crossProduct, 'function', 'dotProduct must be a function');
|
2018-10-04 13:37:37 +00:00
|
|
|
- text: dotProduct() must return null
|
2018-10-20 18:02:47 +00:00
|
|
|
testString: assert.equal(crossProduct(), null, 'dotProduct() must return null');
|
|
|
|
- text: crossProduct([1, 2, 3], [4, 5, 6]) must return [-3, 6, -3].
|
|
|
|
testString: assert.deepEqual(res12, exp12, 'crossProduct([1, 2, 3], [4, 5, 6]) must return [-3, 6, -3].');
|
2018-09-30 22:01:58 +00:00
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function crossProduct() {
|
|
|
|
// Good luck!
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
### After Test
|
|
|
|
<div id='js-teardown'>
|
|
|
|
|
|
|
|
```js
|
2018-10-20 18:02:47 +00:00
|
|
|
const tv1 = [1, 2, 3];
|
|
|
|
const tv2 = [4, 5, 6];
|
|
|
|
const res12 = crossProduct(tv1, tv2);
|
|
|
|
const exp12 = [-3, 6, -3];
|
2018-09-30 22:01:58 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
function crossProduct(a, b) {
|
|
|
|
if (!a || !b) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check lengths
|
|
|
|
if (a.length !== 3 || b.length !== 3) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
(a[1] * b[2]) - (a[2] * b[1]),
|
|
|
|
(a[2] * b[0]) - (a[0] * b[2]),
|
|
|
|
(a[0] * b[1]) - (a[1] * b[0])
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|