Currently delving into algorithms to grasp permutations in javascript, I stumbled upon the following one that left me intrigued:
var permArr = [], usedChars = [];
function permute(input) {
var i, ch, chars = input.split('');
for (i = 0; i < chars.length; i++) {
ch = chars.splice(i, 1);
usedChars.push(ch);
if (chars.length == 0) permArr[permArr.length] = usedChars.join('');
permute(chars.join(""));
chars.splice(i, 0, ch);
usedChars.pop();
}
return permArr
}
For reference, I came across this algorithm on:
While it's evident that this algorithm functions as intended, there is a specific line that poses confusion and eludes clarity:
var i, ch, chars = input.split("");
Upon logging 'i' or 'ch' either before or after in the code, both variables consistently yield undefined results. Interestingly, removing these variables significantly impairs the algorithm's functionality.
I seek elucidation regarding this particular line and its operative mechanism. Any insights would be greatly appreciated!