아래 순열,조합,중복순열을 보면 모든 로직이 다 같지만 next만 로직이 다르다는것을 알 수 있음.
단순히 템플릿을 사용하지말고, 어느 경우에 어떤 것을 사용하는지 확인이 필요
const getPermutations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((e) => [e]);
arr.forEach((fixed, index) => {
const next = [...arr.slice(0, index), ...arr.slice(index+1)]
const permutations = getPermutations(next, selectNumber - 1);
const result = permutations.map((e) => [fixed, ...e]);
results.push(...result);
});
return results;
}
const getCombinations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((e) => [e]);
arr.forEach((fixed, index) => {
const next = arr.slice(index + 1);
const combinations = getCombinations(next, selectNumber - 1);
const result = combinations.map((e) => [fixed, ...e]);
results.push(...result);
});
return results;
}
const getPermutations = function (arr, selectNumber) {
const results = [];
if (selectNumber === 1) return arr.map((e) => [e]);
arr.forEach((fixed, index) => {
const permutations = getPermutations(arr, selectNumber - 1);
const result = permutations.map((e) => [fixed, ...e]);
results.push(...result);
});
return results;
}