I'm struggling with kata called permutation by number by giving an input a word and number (k) the task is returning the kth permutation , actually my function support the unique exp( the 99th permutation of ACGIM = MAGIC) letters how can I enhance it to support the non-repeated letters?
function permutationByNumber(str,index) {
let strr = str.split('').sort().join('')
function fac(integ){
// i made this factorial function to get all factorial numbers
if(integ==1) return 1
return integ*fac(integ-1) }
let index1
let index2
let result =[]
let l = strr.length
function getpermutation(strr,index){
if(result.length==l-1){
return result.push(strr)}
index1=Math.floor(index/fac(strr.length-1))
index2=Math.floor(index%fac(strr.length-1))
result.push(strr[index1])
strr=strr.slice(0,index1)+strr.slice(index1+1)
getpermutation(strr,index2)
}
getpermutation(strr,index)
return result.join('')
}
console.log(permutationByNumber("ACGIM",99))