Can anyone help me with showing all permutations of a value entered into an input field, with a maximum length of 5 characters?
I tried creating a Codepen example for this, but it's not working as expected.
This is the script I used:
<script>
function permute(a) {
if (a.length < 5) return [a];
var c, d, b = [];
for (c = 0; c < a.length; c++) {
var e = a.splice(c, 1),
f = permute(a);
for (d = 0; d < f.length; d++) b.push([e].concat(f[d]));
a.splice(c, 0, e[0])
}
return b
}
function permuteval() {
var txtval = document.getElementById('permute_this').value;
document.getElementById('results').innerHTML =
(permute([txtval]).join("\n"));
}
</script>
Any assistance would be greatly appreciated. Thank you!