permutation

by Paco86

JavaScript

function permutationWord(word, permutations = []){
	if(word.length === 0){
		permutations.push('');
    return permutations;
  }
  
  let first = word.substring(0, 1);
  let remain = word.substring(1);
  
  let subPermutations = permutationWord(remain);
  subPermutations.forEach(function(subPermutation){
  	for(let i = 0; i <= subPermutation.length; i++){
      permutations.push(subPermutation.substring(i,0) + first + subPermutation.substring(i)); 
    }
  })
  
  return permutations;
}

console.log(permutationWord('abc'));