From 2382225e033dc612e9688158212bee78a5b06f0f Mon Sep 17 00:00:00 2001 From: Oleksii Trekhleb Date: Wed, 5 Dec 2018 06:06:29 +0200 Subject: [PATCH] Add comments to Cartesian Product function. --- .../sets/cartesian-product/cartesianProduct.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/algorithms/sets/cartesian-product/cartesianProduct.js b/src/algorithms/sets/cartesian-product/cartesianProduct.js index 61666819..14924fae 100644 --- a/src/algorithms/sets/cartesian-product/cartesianProduct.js +++ b/src/algorithms/sets/cartesian-product/cartesianProduct.js @@ -1,15 +1,27 @@ +/** + * Generates Cartesian Product of two sets. + * @param {*[]} setA + * @param {*[]} setB + * @return {*[]} + */ export default function cartesianProduct(setA, setB) { + // Check if input sets are not empty. + // Otherwise return null since we can't generate Cartesian Product out of them. if (!setA || !setB || !setA.length || !setB.length) { return null; } + // Init product set. const product = []; + // Now, let's go through all elements of a first and second set and form all possible pairs. for (let indexA = 0; indexA < setA.length; indexA += 1) { for (let indexB = 0; indexB < setB.length; indexB += 1) { + // Add current product pair to the product set. product.push([setA[indexA], setB[indexB]]); } } + // Return cartesian product set. return product; }