0

Looking for a way to sequentially find different arrangement possibilities of an array. I only care about adding them sequentially, doesn't need skip or shuffle values.

Example:

var array = [a, b, c, d, e, f];

Desired Output:

a
ab
abc
abcd
abcde
abcdef

It needs to be inside of a loop so I can do a calculation with each possible output.

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Jon Lamb
  • 1,413
  • 2
  • 16
  • 28

4 Answers4

1

You could iterate over each character once and should be able to populate all sequences.

Here is what you could do.

var inputArray = ['a', 'b', 'c', 'd', 'e', 'f'];

var outputStrings = [];

inputArray.forEach((item, idx) => {
  let prevString = (idx !== 0) ? outputStrings[idx - 1] : "";
  outputStrings.push(prevString + item);
});

console.log(outputStrings);
Sreekanth
  • 3,110
  • 10
  • 22
0

You can easily reduce the array by slicing the array to the current index.

var inputArray = ['a', 'b', 'c', 'd', 'e', 'f'];

var outputArray = inputArray.reduce(function(result, item, index, arr) {
  return result.concat(arr.slice(0, index + 1).join(''));
}, []);


document.body.innerHTML = '<pre>' + outputArray.join('\n') + '</pre>';

Note: I am still not sure what you mean by "find different arrangement possibilities of an array".

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

var array = ['a', 'b', 'c', 'd', 'e', 'f'];
results = [];
for (x = 1; x < array.length + 1; ++x) {
  results.push(array.slice(0, x).toString().replace(/,/g, ""))
}
//PRINT ALL RESULTS IN THE RESULTS VARIABLE
for (x = 0; x < results.length; ++x) {
  console.log(results[x])
}
repzero
  • 8,254
  • 2
  • 18
  • 40
0

You need a recursive function to do this.
Since the amount of possible arrangement of your array is 6! (which is 720), I will shorten it to 3 to shorten the sample result, make the number of possible arrangement to 3! (which is 6)

var array = ['a', 'b', 'c'];
var counter = 0; //This is to count the number of arrangement possibilities

permutation();
console.log(counter); //Prints the number of possibilities

function permutation(startWith){
    startWith = startWith || '';
    for (let i = 0; i < array.length; i++){
        //If the current character is not used in 'startWith'
        if (startWith.search(array[i]) == -1){
            console.log(startWith + array[i]); //Print the string
                
            //If this is one of the arrangement posibilities
            if ((startWith + array[i]).length == array.length){
                counter++;
            }
                
            //If the console gives you "Maximum call stack size exceeded" error
            //use 'asyncPermutation' instead
            //but it might not give you the desire output
            //asyncPermutation(startWith + array[i]);
            permutation(startWith + array[i]);
        }
        else { 
            continue; //Skip every line of codes below and continue with the next iteration
        } 
    }
    function asyncPermutation(input){
        setTimeout(function(){permutation(input);},0);
    }
 }

The first 3 output is part of your desired output. Hope this answers your question.

Thum Choon Tat
  • 3,084
  • 1
  • 22
  • 24