Skip to content

Commit 69defd7

Browse files
committed
Add cartesian product
1 parent 3ff7445 commit 69defd7

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var cartesianProduct = (function () {
2+
'use strict';
3+
var result;
4+
5+
function cartesianProduct(sets, index, current) {
6+
if (index === sets.length) {
7+
return result.push(current.slice());
8+
}
9+
for (var i = 0; i < sets[index].length; i += 1) {
10+
current[index] = sets[index][i];
11+
cartesianProduct(sets, index + 1, current);
12+
}
13+
}
14+
15+
return function (sets) {
16+
result = [];
17+
cartesianProduct(sets, 0, []);
18+
return result;
19+
};
20+
}());
21+
22+
// console.log([[1, 2],[3, 4],[5, 6],[7, 8]]);
23+
// console.log(cartesianProduct([[1, 2],[3, 4],[5, 6],[7, 8]]));

0 commit comments

Comments
 (0)