Problem 1
I have this variables
var myArr = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
var nOptions = 10;
and I'm trying to get all unique possible combination with 10 options.
Results example:
var result1 = ["A", "A", "A", "A", "A", "A", "A", "A", "A", "A"];
var result2 = ["A", "A", "A", "A", "A", "A", "A", "A", "A", "B"];
var result3 = ["A", "A", "A", "A", "A", "A", "A", "A", "B", "A"];
var result4 = ["A", "A", "A", "A", "A", "A", "A", "B", "A", "A"];
----------------------------------------------------------------
var result5 = ["A", "A", "A", "A", "A", "A", "A", "A", "B", "B"];
var result6 = ["A", "A", "A", "A", "A", "A", "A", "B", "A", "B"];
var result7 = ["A", "A", "A", "A", "A", "A", "B", "A", "A", "B"];
----------------------------------------------------------------
var result8 = ["A", "A", "A", "A", "A", "A", "A", "A", "C", "B"];
var result9 = ["A", "A", "A", "A", "A", "A", "A", "C", "A", "B"];
var result10 = ["A", "A", "A", "A", "A", "A", "C", "A", "A", "B"];
----------------------------------------------------------------
var result11 = ["A", "A", "A", "A", "A", "A", "A", "C", "B", "B"];
var result12 = ["A", "A", "A", "A", "A", "A", "C", "A", "B", "B"];
var result13 = ["A", "A", "A", "A", "A", "C", "A", "A", "B", "B"];
----------------------------------------------------------------
var resultn = ["I", "I", "I", "I", "I", "I", "I", "I", "I", "I"];
Is there any way to print all that results in JavaScript?
Is there a math formula so I can know the number of all combinations without making all the loops?
Problem 2
Finally I want to make a filter so I can have all possible combinations with the new filters. Example:
var minA = 0;
var maxA = 3;
var minB = 2;
var maxB = 4;
var minC = 0;
var maxC = 3;
-------------
var minI = 3;
var maxI = 5;
Example valid results with filter:
var result1 = ["B", "B", "B", "B", "I", "I", "I", "I", "I", "A"];
var result2 = ["B", "B", "B", "B", "I", "I", "I", "I", "I", "C"];
var result3 = ["B", "B", "B", "B", "I", "I", "I", "I", "A", "A"];
Is there any way to print all filter results in JavaScript directly or I have to loop throw all results and make counters for each letter and then if all counters are valid with the filter options print the new results?
Is there a math formula so I can know the number of all combinations with filters without making all the loops?
Thanks to everybody who can help in making the code. I will update the code so everybody can use it for future projects.
CODE Problem 1
var myArr = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
var nOptions = 10;
myCounter = 0;
allResults = [];
for (let a = 0; a < 9; a ) {
for (let b = 0; b < 9; b ) {
for (let c = 0; c < 9; c ) {
for (let d = 0; d < 9; d ) {
for (let e = 0; e < 9; e ) {
for (let f = 0; f < 9; f ) {
for (let g = 0; g < 9; g ) {
for (let h = 0; h < 9; h ) {
for (let i = 0; i < 9; i ) {
for (let j = 0; j < 9; j ) {
allResults.push([myArr[a], myArr[b], myArr[c], myArr[d], myArr[e], myArr[f], myArr[g], myArr[h], myArr[i], myArr[j]]);
myCounter ;
}
//break;
}
//break;
}
//break;
}
//break;
}
//break;
}
//break;
}
//break;
}
//break;
}
//break;
}
Better CODE Problem 1
I made a better code so now nOptions can be a reasonable number (min 0, max 26) but a number too big will have too much possible combinations and the pc couldn't process all that data. A maxium reasonable number would be nOptions = 6. That means 9^6 = 531.441 results.
var myArr = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
var nOptions = 2;
var myCounter = 0;
var allResults = [];
var myLetters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
codeLine='';
for (let i = 0; i < nOptions; i ) {
codeLine = 'for (let ' myLetters[i] ' = 0; ' myLetters[i] ' < 9; ' myLetters[i] ' ) {';
}
codeLine = 'allResults.push([';
for (let i = 0; i < nOptions; i ) {
codeLine = 'myArr[' myLetters[i] ']';
if (i !== nOptions - 1) {
codeLine = ', ';
}
}
codeLine = ']);';
codeLine = 'myCounter ;';
//codeLine = 'console.log(myCounter);';
for (let i = 0; i < nOptions; i ) {
codeLine = '}';
}
//console.log(codeLine);
eval(codeLine);
console.table(allResults);
CodePudding user response:
Solution to problem 1#
var myArr = ["A", "B", "C", "D", "E", "F", "G", "H", "I"];
var nOptions = 2;
var myCounter = 0;
var allResults = [];
var myLetters = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
codeLine='';
for (let i = 0; i < nOptions; i ) {
codeLine = 'for (let ' myLetters[i] ' = 0; ' myLetters[i] ' < 9; ' myLetters[i] ' ) {';
}
codeLine = 'allResults.push([';
for (let i = 0; i < nOptions; i ) {
codeLine = 'myArr[' myLetters[i] ']';
if (i !== nOptions - 1) {
codeLine = ', ';
}
}
codeLine = ']);';
codeLine = 'myCounter ;';
//codeLine = 'console.log(myCounter);';
for (let i = 0; i < nOptions; i ) {
codeLine = '}';
}
//console.log(codeLine);
eval(codeLine);
console.table(allResults);
CodePudding user response:
You can convert between numbers and letters by their digit. Here you have 9 letters you care about, so useing base 9 we have: 0=a, 1=b, ... 8=i.
Then use toString to convert all the integers from 0 to 9^11-1 to base 9, with leading 0s. Finally, convert the digits back to the appropriate letters.
0 -> 0000000000 -> aaaaaaaaaa
1 -> 0000000001 -> aaaaaaaaab
...
64340123 -> 0064340123 -> aagedeabcd
...
8888888888 -> 8888888888 -> iiiiiiiiii