Ferrius
Ferrius

Reputation: 69

How to calculate all the combinations between different arrays?

Let's say I have this 3 arrays:

 Shirts [White, Navy, Light Blue, Gray],
 Pants [Black, Navy, Gray],
 Ties [Houndstooth, Polka Dot, Herringbone, Solid]

What should I do to get this result

 White Shirt with Black Pants and a Houndstooth Tie,
 White Shirt with Black Pants and a Polka Dot Tie,
 White Shirt with Black Pants and a Herringbone Tie,
 White Shirt with Black Pants and a Solid Tie,

 White Shirt with Navy Pants and a Houndstooth Tie,
 White Shirt with Navy Pants and a Polka Dot Tie,
 White Shirt with Navy Pants and a Herringbone Tie,
 White Shirt with Navy Pants and a Solid Tie,

 And so on,
 And so on...

Upvotes: 0

Views: 226

Answers (2)

Obsidian Age
Obsidian Age

Reputation: 42384

Simply loop over one of them, outputting the index for each array. You'll need to use loops inside of loops for this, using differing indexes for each loop:

var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];

for (var i = 0; i < shirts.length; i++) {
  var start = shirts[i] + " shirt with ";
  for (var j = 0; j < pants.length; j++) {
    var middle = pants[j] + " pants and a ";
    for (var k= 0; k < ties.length; k++) {
      var end = ties[k] + " tie.<br />";
     document.write(start + middle + end);
    }
  }
}

You can get the total by simply multiplying the .length of the three arrays together, assigning the total to a variable, and then outputting that variable:

var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];
var total_combinations = shirts.length * pants.length * ties.length;
document.write("Total number of combinations: " + total_combinations);

To get a random output, you can use Math.floor() in conjunction with Math.random() on the array indexes as follows:

var shirts = ["White", "Navy", "Light Blue", "Gray"];
var pants = ["Black", "Navy", "Gray"];
var ties = ["Houndstooth", "Polka Dot", "Herringbone", "Solid"];

var random_shirt = shirts[Math.floor(Math.random()*shirts.length)];
var random_pants = pants[Math.floor(Math.random()*pants.length)];
var random_tie = ties[Math.floor(Math.random()*ties.length)];

document.write(random_shirt + " shirt with " + random_pants + " pants and a " + random_tie + " tie.");

Hope this helps! :)

Upvotes: 2

shukshin.ivan
shukshin.ivan

Reputation: 11340

Just to calculate? That's easy.

Multiply them. 5*3*4=60 variants.

If you need to have all the variants as a text:

var shirts = ['White', 'Navy', 'Light Blue', 'Gray'];
    var pants = ['Black', 'Navy', 'Grey'];
    var ties = ['Houndstooth', 'Polka Dot', 'Herringbone', 'Solid'];
    
    for(var s in shirts) {
      for(var p in pants) {
        for(var t in ties) {
            document.write(shirts[s] + ' Shirt with ' + pants[p] + ' Pants and a ' + ties[t] + ' Tie<br>');
        }
      } 
    }

Upvotes: 2

Related Questions