hey guys unforunately my function work only with the first lettre for exple if i wanna get the 16th permutation index of input 'abcd' i got 'cabd' instead of 'cbda' i mean the c index in place but i didn't find a way to flip the 'abd'cases , idk if i can maybe recall the function by slicing by one (recursion call) please how i can enhance it
function permutationByNumber(str,index) {
const 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 count = 0
// i made this count variable by increasing it by one in each time the loop iterate
for(let i=fac(strr.length)/strr.length ;i<=fac(strr.length);i+=fac(strr.length)/strr.length){
if(i>=index){
break
}
count++
}
let rest = strr.slice(0,count) + strr.slice(count+1)
return strr[count] + rest }
console.log(permutationByNumber('abcd',16))