Skip to content

Commit fcd4598

Browse files
committed
Cartesian algorithm
1 parent d1840c1 commit fcd4598

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/sets/sets.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Set - a collection of values which forms an entity itself, most arrays are sets ( connected values )
2+
3+
// Set Algorithm Example: Cartesian Product Algorithm:
4+
export const getCartesianProducts = (colors, sizes) => {
5+
const options = [];
6+
for (let c = 0; c < colors.length; c++) {
7+
for (let s = 0; s < sizes.length; s++) {
8+
options.push([colors[c], sizes[s]]);
9+
}
10+
}
11+
return options;
12+
};

src/sets/sets.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { getCartesianProducts } from "./sets";
2+
3+
describe("Cartesian Product", () => {
4+
test("Cartesian Product", () => {
5+
expect(
6+
getCartesianProducts(["blue", "red"], ["S", "M", "L"])
7+
).toStrictEqual([
8+
["blue", "S"],
9+
["blue", "M"],
10+
["blue", "L"],
11+
["red", "S"],
12+
["red", "M"],
13+
["red", "L"],
14+
]);
15+
});
16+
test("Cartesian Product", () => {
17+
expect(
18+
getCartesianProducts(["blue", "red", "white"], ["S", "M", "L"])
19+
).toStrictEqual([
20+
["blue", "S"],
21+
["blue", "M"],
22+
["blue", "L"],
23+
["red", "S"],
24+
["red", "M"],
25+
["red", "L"],
26+
["white", "S"],
27+
["white", "M"],
28+
["white", "L"],
29+
]);
30+
});
31+
});

0 commit comments

Comments
 (0)