Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2382225

Browse files
committed
Add comments to Cartesian Product function.
1 parent 243be8f commit 2382225

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
1+
/**
2+
* Generates Cartesian Product of two sets.
3+
* @param {*[]} setA
4+
* @param {*[]} setB
5+
* @return {*[]}
6+
*/
17
export default function cartesianProduct(setA, setB) {
8+
// Check if input sets are not empty.
9+
// Otherwise return null since we can't generate Cartesian Product out of them.
210
if (!setA || !setB || !setA.length || !setB.length) {
311
return null;
412
}
513

14+
// Init product set.
615
const product = [];
716

17+
// Now, let's go through all elements of a first and second set and form all possible pairs.
818
for (let indexA = 0; indexA < setA.length; indexA += 1) {
919
for (let indexB = 0; indexB < setB.length; indexB += 1) {
20+
// Add current product pair to the product set.
1021
product.push([setA[indexA], setB[indexB]]);
1122
}
1223
}
1324

25+
// Return cartesian product set.
1426
return product;
1527
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /