This code uses two nested loops to iterate over the array and generate all possible pairs of items. The outer loop iterates over each item in the array, and the inner loop iterates over the remaining items in the array to generate all pairs that include the current item. The pairs are stored in an array called pairs, which is logged to the console at the end of the code.
The output of this code for the given items would be something like:
This will produce:
Note that our code generates all possible pairs of items without any duplicates, meaning that each pair is unique and does not appear in reverse order.
Alternatively, you can simply update the code to say:
Here we are doing console.log() on each element of “pairs”. The output will be:
This code uses three nested loops to iterate over the array and generate all possible triples of items. The outer loop iterates over each item in the array, the second loop iterates over the remaining items in the array to generate all pairs that include the current item, and the innermost loop iterates over the further remaining items in the array to generate all triples that include the current pair. The triples are stored in an array called triples which looks like:
And thus the console.log() statement will print each element of this array (which is itself an array) on a separate line.
The above gets cumbersome if we want to generate combinations of all sizes, not just a fixed size. To generate combinations of all sizes from the given array, we can use something like the following:
This code generates all possible combinations of sizes 1 to n from the given array, where n is the length of the array. The combinations are stored in an array called combinations, which is logged to the console at the end of the code.
The output of this code for the given items would be elements of this array:
As you can see, the code generates all possible combinations of sizes 1 to 5 from the given array, where each combination is unique and does not contain any duplicate items or the same items in different orders.
In this approach, we explore a recursive strategy. The below code generates all possible combinations of the items in the items array by calling the generateCombinations function for each possible combination size, from 1 to the length of the items array. Note that the generateCombinations function uses recursion to generate all possible combinations of the given size. It does this by iterating through the arr array, starting at the start index, and adding each element to the temp array. If the temp array has reached the desired size, it adds a copy of the temp array to the combinations array. If the temp array has not reached the desired size, it calls itself recursively with the next index as the start index and the updated temp array. After all combinations have been generated, the combinations array is printed to the console using a forEach loop as usual.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.