I am new to Javascript and I was trying to reproduce a Java string permutation using Javascript but I cannot get the correct result.
Note: I followed this algorithm here (Hemant's answer): String permutation with recursion
Here is my code:
function getAllPermutations(input){
var permutations = [];
function loop(prefix, input){
var len = input.length;
if(len==0) {
permutations.push(prefix);
}
else{
for(i = 0; i < len; i++){
loop(prefix + input.charAt(i),input.substr(0,i) + input.substr(i+1,len));
}
}
}
loop("", input);
return permutations;
}
When I call getAllPermutations("good"), it's only returning ["good"]. What's going on with this code? Am I missing anything about the call back scope?