const permutations = (arr, num) => {
const result = [];
const perm = (arr, n, cur) => {
for (let i = 0; i < arr.length; i += 1) {
cur.push(arr[i]);
if (n === 1) {
result.push([...cur]);
} else {
newArr = [...arr];
newArr.splice(i, 1);
perm(newArr, n - 1, cur);
}
cur.pop();
}
};
perm(arr, num, []);
return result;
}
console.log(permutations([1, 2, 3, 4], 3));
// Array(24) [ (3) […], (3) […], (3) […], (3) […], (3) […], (3) […], (3) […], (3) […], (3) […], (3) […], … ]
// 0: Array(3) [ 1, 2, 3 ]
// 1: Array(3) [ 1, 2, 4 ]
// 2: Array(3) [ 1, 3, 2 ]
// 3: Array(3) [ 1, 3, 4 ]
// 4: Array(3) [ 1, 4, 2 ]
// 5: Array(3) [ 1, 4, 3 ]
// 6: Array(3) [ 2, 1, 3 ]
// 7: Array(3) [ 2, 1, 4 ]
// ...
яж говорю, не шарю :-)Тут не гордиться надо, а стыдиться.
const result = [1, 2, 3].map((x) => x + 1);
const result = [2, 3, 4];
function mapper(f) {
const foo = (a) => map(a, f);
return foo;
}
grep -i redpils characters