mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-07 06:02:06 -04:00
1.3 KiB
1.3 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 594810f028c0303b75339ad2 | Producto Cruz de Vectores | 1 | 302342 | vector-cross-product |
--description--
Un vector se dice de tres dimensiones cuando es representado por una colección ordenada de tres números: (X, Y, Z).
--instructions--
Escriba una función que tome dos vectores (arreglos) como entrada y calcule su producto cruz. Tu función debe devolver null en entradas inválidas como vectores de diferentes longitud.
--hints--
crossProduct debe ser una función.
assert.equal(typeof crossProduct, 'function');
crossProduct() debe retornar null.
assert.equal(crossProduct(), null);
crossProduct([1, 2, 3], [4, 5, 6]) debe retornar [-3, 6, -3].
assert.deepEqual(res12, exp12);
--seed--
--after-user-code--
const tv1 = [1, 2, 3];
const tv2 = [4, 5, 6];
const res12 = crossProduct(tv1, tv2);
const exp12 = [-3, 6, -3];
--seed-contents--
function crossProduct(a, b) {
}
--solutions--
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])
];
}