Julia
Julia

Reputation: 509

Generating Different Combinations of an Array of Strings [JavaScript]

How can I generate in JavaScript all the different combinations between the elements of an array of strings with the following conditions:

For instance, with this input array of strings:

var array = ["A", "B", "C"];

The different combinations would only be:

I pretend to use it to do something like this:

var count = 0;
for each (different combinations in input array of strings){
   console.log (item1 of combination);
   console.log (item2 of combination);
   count = count + 1;
}
console.log(count);

Thank you *

Upvotes: 1

Views: 68

Answers (1)

Dij
Dij

Reputation: 9808

Something like this would work.

var arr = ["A", "B", "C"];
var count = 0;
for (var i=0; i<arr.length; i++){
   for (var j=i+1; j<arr.length; j++){
      console.log(arr[i] + arr[j]);
      count = count + 1;
   }
}
console.log(count);

Upvotes: 2

Related Questions