Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

474
Views
how i can get the nth permutation in javascript (codewars task)

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))
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!