0

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))
  • 2
    Does this answer your question? [Find ith permutation in javascript](https://stackoverflow.com/questions/40441586/find-ith-permutation-in-javascript) – Ben Dec 05 '21 at 02:15
  • hmm thank you so much , actually if there is a way to enhance my function that would be better as u can see my function return the first the first index output in place but i wanna get the rest of permutation if it' possible ofc – med azzouzi Dec 05 '21 at 15:40

0 Answers0