v1
- by souljorje 4/1/202100
Setup HTML - click to add setup HTML
disable setup JavaScript
Setup JavaScript
const foo = [1, 2];
const bar = [10, 20];
const baz = [100, 200, 300];
const fooBarBaz = [foo, bar, baz];

// https://stackoverflow.com/a/66889540/12496886
const getAllCombinations = (arraysToCombine) => {
  const divisors = [];
  let combinationsCount = 1;
  for (let i = arraysToCombine.length - 1; i >= 0; i--) {
      divisors[i] = divisors[i + 1] ? divisors[i + 1] * arraysToCombine[i + 1].length : 1;
      combinationsCount *= (arraysToCombine[i].length || 1);
  }

  const getCombination = (n, arrays, divisors) => arrays.reduce((acc, arr, i) => {
      acc.push(arr[Math.floor(n / divisors[i]) % arr.length]);
      return acc;
  }, []);

  const combinations = [];
  for (let i = 0; i < combinationsCount; i++) {
      combinations.push(getCombination(i, arraysToCombine, divisors));
  }
  return combinations;
};

// https://stackoverflow.com/a/43053803/12496886
const shortVersion = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));

// https://stackoverflow.com/a/36234242/12496886
function cartesianProduct (arr) {
  return arr.reduce((a, b) => {
    return a.map(x => {
      return b.map(y => {
        return x.concat(y)
      })
    }).reduce((c, d) => c.concat(d), [])
  }, [[]])
}
delete caserun single casemove downdrag and drop case


ready



getAllCombinations(fooBarBaz);
delete caserun single casemove upmove downdrag and drop case


ready



shortVersion(foo, bar, baz);
delete caserun single casemove updrag and drop case


ready



cartesianProduct(fooBarBaz);
Test Case - click to add another test case
Teardown JS - click to add teardown JavaScript
Output (DOM) - click to monitor output (DOM) while test is running
RUN