Katakanlah saya memiliki 3 array ini:

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

Apa yang harus saya lakukan untuk mendapatkan hasil ini?

 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...
0
Ferrius 16 Agustus 2017, 02:28

2 jawaban

Jawaban Terbaik

Cukup lingkari salah satunya, keluarkan indeks untuk setiap larik. Anda harus menggunakan loop di dalam loop untuk ini, menggunakan indeks yang berbeda untuk setiap 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);
    }
  }
}

Anda bisa mendapatkan total hanya dengan mengalikan .length dari tiga larik bersama-sama, menetapkan total ke variabel, lalu mengeluarkan variabel tersebut:

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

Untuk mendapatkan keluaran acak, Anda dapat menggunakan Math.floor() bersama dengan Math.random() pada indeks array sebagai berikut:

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

Semoga ini membantu! :)

2
Obsidian Age 15 Agustus 2017, 23:53

Hanya untuk menghitung? Itu mudah.

Kalikan mereka. 5*3*4=60 varian.

Jika Anda perlu memiliki semua varian sebagai teks:

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>');
        }
      } 
    }
2
shukshin.ivan 15 Agustus 2017, 23:36