0

I'm creating a password generator for learning purposes.

first-input is my input field where I generate the random string of items in the html.

I'm using a function here because there is a button that triggers it to put the random string of items inside the input field. I found this function in stackoverflow and this function generates 15 random items from the array, but it puts a comma after each item. How do I get rid of the comma? It logs out stuff like:

J,l,9,),6,A,X,e,_,*,[,x,7,1,&

And I need:

Jl9)6AXe_*[x71&

Now, if I want to take a certain number of items from that array without including a specific group of items like capital letters or numbers, or symbols... how would I exclude them from the array? The goal is to check a box and it excludes that group from the generator. Do I split the array somehow? Make multiple arrays that consist of specific symbols only? Is there a simple way to do this?

const characters = [
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?","/"
];

let firstPass = document.getElementById("first-input");

function firstPassword() {
  const shuffledChar = characters.sort(() => 0.5 - Math.random());
  const getRandom = (arr, min, max) => {
    const numItemsToPick = Math.floor(Math.random() * (max-min+1)) + min
    return arr.slice(0, numItemsToPick)
  }
  firstPass.value = getRandom(shuffledChar, 15,15)
}

1 Answers1

0

A decent way to organize the different character sets would be to put them into a multidimensional array (characterSets). You can then grab the inputs, loop through them, and if they are not checked, include their corresponding character subset by using the array method concat. My example below is assuming that we have 4 inputs with class of 'my-checkbox'. To get the output without commas, use the array method, join. Also, for shuffling arrays, please see How to randomize (shuffle) a JavaScript array?. The shuffledChar array is not truly random.

const characterSets = [
    ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],
    ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],
    ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"],
    ["~","`","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","}","]",",","|",":",";","<",">",".","?","/"]
];

let firstPass = document.getElementById("first-input");

function firstPassword() {
    let checkboxes = document.querySelectorAll('.my-checkbox');
    let selectedCharSets = [];
    for (let i = 0; i < checkboxes.length; i++) {
        if (!checkboxes[i].checked) {
            selectedCharSets = selectedCharSets.concat(characterSets[i]);
        }
    }

    const shuffledChar = selectedCharSets.sort(() => 0.5 - Math.random());
    const getRandom = (arr, min, max) => {
        const numItemsToPick = Math.floor(Math.random() * (max-min+1)) + min
        return arr.slice(0, numItemsToPick)
    }
    firstPass.value = getRandom(shuffledChar, 15,15).join("")
}
Zbow
  • 160
  • 8