I'm reading some algorithms to try and understand permutations in javascript, and the following one quite stunned me
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
}
FYI I found this algorithm on the following website: http://staff.roguecc.edu/JMiller/JavaScript/permute.html
I can see that this algorithm works, but there is a line that confuses me and I can't find anywhere where it works
var i, ch, chars = input.split("");
If i console.log(i) or console.log(ch) before OR after in the code, it returns undefined everywhere. If i remove i and ch, the algorithm does not work anymore.
Can someone explain me this line and how it works? Thanks a lot