Guarantee the Selected Character Types
Guarantee the Selected Character Types
A combined pool alone does not guarantee that every selected category appears. We will deliberately add one character from each enabled category before filling the remaining positions.
1. Handle an empty selection
At the top of generatePassword(), after creating enabled, add:
if (!enabled.length) {
$("lowercase").checked = true;
enabled.push("lowercase");
}
This gives the generator a safe fallback instead of leaving it with an empty pool.
2. Create the character array
Before the loop that builds pool, add:
const characters = [];
Inside the existing enabled.forEach() loop, keep the pool-building line and add one guaranteed random character:
characters.push(sets[id][secureRandom(sets[id].length)]);
Now the loop has two jobs: it adds the selected set to the pool and guarantees one character from that set.
3. Observe the intermediate result
Temporarily log characters after the loop. With lowercase + numbers selected, you should see two characters: one from each category.
Do not worry about the final password length yet. We will solve that next.
Test
Try one selected category, two categories, and all four. Confirm the number of guaranteed characters equals the number of enabled categories.
Remove the temporary log.
Checkpoint
The generator now deliberately includes at least one character from every selected category instead of hoping random selection will do so.