-4

So I have an array of (in my case it will be digits only, but I don't think that's very relevant) integers and I wanna get an array of all possible arrangements of this array so let's say

    arr = [1,4,5]
allArrangements(arr) 
// [[1,4,5],[1,5,4],[4,1,5],[4,5,1],[5,1,4],[5,4,1]]
coldman1
  • 23
  • 8
  • 3
    Ok so.... what did you try? – jdmdevdotnet May 15 '17 at 14:56
  • 3
    Give it a go. If you run into a **specific** problem doing this yourself, after doing thorough research and [searching SO](/help/searching), if you cannot resolve the problem, post your code and a description of the problem you're having. – T.J. Crowder May 15 '17 at 14:56

1 Answers1

0

You have to multiply the total of items from highest to lowest

  • [1,4,5] > 3*2*1 = 6 possible arrangements
  • [1,4,5,6] > 4*3*2*1 = 24 possible arrangements
  • [1,4,5,6,7] > 5*4*3*2*1 = 120 possible arrangements

var array = [1,4,5,6];
function permutation (arr){
  var len = arr.length,
      i = len-1,
      ags = len;
  while(i--){
    len--;
    ags *= len;
  }
  return ags;
}
console.log(permutation(array));
alessandrio
  • 4,282
  • 2
  • 29
  • 40
  • 1
    I think OP is looking for the permutations themselves, not the number of permutations. – larz May 15 '17 at 15:30