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.

2
  • 1
    what are a, b, c? variables? strings? pieces of lint? Commented Nov 16, 2016 at 3:58
  • is that all the desired output? What about the characters in a different order? Commented Nov 16, 2016 at 4:02

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

Comments

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".

Comments

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]) }

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.