Coding Challenge: Permutations in JavaScript

Question: Return Permutations for given string.

Answer:

function getPermute(string) {

var output = [];

if (string.length === 1) {

output.push(string);
return output;

}

for (var i = 0; i < string.length; i++) {

var firstChar = string[i];
var charsLeft = string.substring(0, i) + string.substring(i + 1);
var permute = getPermute(charsLeft);

for (var j = 0; j < permute.length; j++) {

output.push(firstChar + permute[j]);

}

}

return output;

}

Try it yourself: