Files
freeCodeCamp/curriculum/challenges/german/22-rosetta-code/rosetta-code-challenges/vector-cross-product.md
2024-01-24 19:52:36 +01:00

1.3 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
594810f028c0303b75339ad2 Vektorielles Kreuzprodukt 1 302342 vector-cross-product

--description--

A vector is defined as having three dimensions as being represented by an ordered collection of three numbers: (X, Y, Z).

--instructions--

Schreibe eine Funktion, die zwei Vektoren (Anordnungen) als Eingabe nimmt und ihr Kreuzprodukt berechnet. Deine Funktion sollte bei ungültigen Eingaben, wie Vektoren unterschiedlicher Länge, null zurückgeben.

--hints--

crossProduct sollte eine Funktion sein.

assert.equal(typeof crossProduct, 'function');

crossProduct() sollte null zurückgeben.

assert.equal(crossProduct(), null);

crossProduct([1, 2, 3], [4, 5, 6]) sollte [-3, 6, -3] zurückgeben.

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])
  ];
}