String Permutations
by LyndseyB
HTML
<ul>
<li>Prefix = A</li>
<li>Prefix = AB</li>
<li>Prefix = AC</li>
</ul>
Babel + JSX
const permute = (string, prefix, permutations = []) => {
if(typeof prefix === 'undefined') {
prefix = string[0];
string = string.replace(prefix, '');
}
if(!permutations.includes(string + prefix)) {
permutations.push(string + prefix);
}
string.split('').forEach((letter) => {
const replacer = string.replace(letter, '');
const permutation = permute(replacer, prefix + letter, permutations);
permutations.concat(permutation);
});
return permutations;
};
const letters = 'abc';
const permuted = permute('abc');
console.log(permuted);