JavaScript CommonJS module/project to calculate all permutations of combinations of HTML content structure
elements set to be in a selected state by their unique identifier matched within each combination
of values (values are two options, an identifier or unset value, at a position in the combination).
The elements would be items within the list of children to another HTML element.
To select one single HTML element from combinations of values where this choice of single element
is selected by being in the “1” (set option) state at its position, in every unique combination
(all combinations with the state value set to “1” in the position of the single target element)
there will be hidden (invisible) label-type HTML elements with a unique combination value as the identifier
that are hidden links to the single element identifier and make the single element show (be visible)
with a : target CSS (styling of HTML) selection code - put in the description CSS code of its
selection within combinations with its state value. This CSS code shows all single selected HTML elements
and is then to be generated by this JavaScript module.
Every combination of state values is to be (hyper-)linked to as an URL fragment that is the combination as
a line of text — an URL fragment is a name preceded by a hash mark (#), which specifies an internal target
location (an id of an HTML element) within the current [HTML] document and it goes last in URL text.
The initial JavaScript program code after this was generated through asking Google Gemini (2.5 Flash)
AI, how to write permutation program code to list the set of ways to change single bit states
in a set of binary combination elements, permuted by position of combination option.
To quote myself with Gemini:
Please show JavaScript program code [that] set[s] a variable
to an empty array then repeat from 1 to an end number and
for each iteration number, calculate the array of all
the permutation combinations of a set of “0” values, with the
set length as the iteration end number, with every combination
having an arrangement of the iteration number of values replaced by
”1” then concatenate this other array of all the permutation arrays
to the first array variable to extend it and increment the iteration
number before advancing to the next iteration if the iteration number
is less than the end number so there are the end number of counted loop iterations.
(this needed much work to know some specific words to express the idea with and imagine the resulting programming)
Quote of Gemini:
I will first write a JavaScript function
generatePermutationsthat takes a set length and the number of ‘1’s as input, and returns an array of all permutation combinations of ‘0’s and ‘1’s as described. Then, I will write the main program code that initializes an empty array. After that, I will loop from 1 to the specified end number, and in each iteration [usesi], I will call thegeneratePermutationsfunction with the current iteration number as both the set length and the number of ‘1’s to replace. Finally, I will concatenate the resulting permutations to the main array and proceed with the loop.
but I want the length of every permutation combination array to be fixed as a length of the end number of iterations.
I asked Gemini to adjust code, they changed the generatePermutations function and main program loop.
and Gemini describes the generatePermutations function, with a recursive combination position process,
to include both remaining combination options with placing a ‘1’ state or ‘0’ state at each position,
by always calling the remaining combination process with both states, while respecting the count of ‘1’ (one)
states being limited to less than the ones Count limit.
This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0, or subsequent versions
but is “Incompatible With Secondary Licenses” as defined by the Mozilla Public License, version 2.0
(to NOT distribute under the terms of licenses of a Larger Work).
#!/bin/env node
function generatePermutations(setLength, onesCountMax) {
const permutations = [];
const currentPermutation = new Array(setLength).fill(0); // This array represents a single permutation being built.
function backtrack(index, countOfOnes) {
// Base case: We've filled all positions in the `currentPermutation` array.
if (index === setLength) {
// If the number of '1's in this completed permutation matches our target `onesCountMax`,
// then it's a valid combination, so we add a copy of it to our results.
if (countOfOnes === onesCountMax) {
permutations.push([...currentPermutation]);
}
return; // Stop this branch of recursion.
}
// --- Option 1: Try to place a '1' at the current `index` ---
// We only proceed with this option if we haven't already placed
// the maximum allowed number of '1's (`onesCountMax`).
if (countOfOnes < onesCountMax) {
currentPermutation[index] = 1; // Place a '1'
// Recursively call backtrack for the next position,
// starting from an incremented count of '1's.
backtrack(index + 1, countOfOnes + 1);
}
// --- Option 2: Try to place a '0' at the current `index` ---
// This option is always available because placing a '0'
// doesn't affect our `onesCountMax` limit.
currentPermutation[index] = 0; // Place a '0'
// Recursively call backtrack for the next position,
// keeping the count of '1's the same.
backtrack(index + 1, countOfOnes);
}
// Initial call to start the backtracking process.
// Start at index 0 and with 0 '1's (ones) counted so far.
backtrack(0, 0);
return permutations;
}
// Main program
let allPermutationArrays = [];
const endNumber = 3; // You can change this to any desired end number.
for (let i = 1; i <= endNumber; i++) {
// For each iteration 'i', generate permutations of a finite set with length of `endNumber`,
// with 'i' number of '0' values replaced by '1's.
const currentIterationPermutations = generatePermutations(endNumber, i);
// Concatenate the current iteration's permutations to the main array
allPermutationArrays = allPermutationArrays.concat(currentIterationPermutations);
console.log(`Iteration ${i} (permutations with ${i} '1's in a set of length ${endNumber}):`);
// console.log(currentIterationPermutations);
// This runs without debugging:
for (let p = 0; p < currentIterationPermutations.length; p++) {
console.log(`(${p+1}) Permutation array: [${currentIterationPermutations[p].toString()}]`);
}
}
console.log("\nAll collected permutation arrays:");
console.log(allPermutationArrays);