Skip to content

Commit 1eebb5d

Browse files
committed
feat: create solution to exercise
1 parent 01d2ec5 commit 1eebb5d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed
Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
const permutations = function() {
2-
// Replace this comment with the solution code
1+
const permutations = function (original, currentPermutations) {
2+
if (original.length === 1) return original;
3+
4+
const perms = currentPermutations
5+
? currentPermutations
6+
: original.map((el) => [el]);
7+
8+
const newPerms = [];
9+
perms.forEach((el) => {
10+
const missing = original.filter((item) => !el.includes(item));
11+
missing.forEach((itemMissing) => newPerms.push([...el, itemMissing]));
12+
});
13+
14+
if (newPerms.every((el) => el.length === original.length)) return newPerms;
15+
return permutations(original, newPerms);
316
};
4-
17+
518
// Do not edit below this line
619
module.exports = permutations;

0 commit comments

Comments
 (0)