I've got this kind of code:
function swap (alphabets, index1, index2) {
var temp = alphabets[index1];
alphabets[index1] = alphabets[index2];
alphabets[index2] = temp;
return alphabets;
}
function permute (alphabets, startIndex, endIndex) {
if (startIndex === endIndex) {
console.log(alphabets.join(''));
} else {
var i;
for (i = startIndex; i <= endIndex; i++) {
swap(alphabets, startIndex, i);
permute(alphabets, startIndex + 1, endIndex);
swap(alphabets, i, startIndex); // backtrack
}
}
}
For example if the input is [ 1, 4, 2 ] the output will be(this output is not correct) 142 142 124 124 124 142 412 412 421 421 421 412 241 241 214 214 214 241 421 412 241 214 124 142 I want this code to return a fixed amount of numbers. Example: 2characters, [ 1, 4, 2 ] => 14, 12, 42, 24, 21, 41 ; 3characters, [ 1, 4, 2 ] => 142, 124, 421, 412 and so on.