1

Disclaimer: I'm extremely new to javascript but this is my line of thought, I want to create a random password generator and I want to store all the possible variables in a single array. So for example I want the array to end up looking like this:

var characters = [
{
numbers: ("0", "1", "2", "3", "4" , "5", "6" , "7", "8", "9"),
specialChar: ("!", "%", "&", ",", "*", "+", "-", ".", "/", "<", ">", "?","~"),
upprCase: ("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"),
lowrCase: ("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"),

My goal is to be able to reference this as a global array and from there create a function to randomize the values inside the array and return the newly generated password. I'm not asking for anyone to make this I just want to know if this line of thinking will work.

3
  • you could take other arrays for the values. Commented Jun 9, 2021 at 20:56
  • well your array inside is not an array [ { numbers: ("0", <-- that ( is not an array. It is unclear why you have the [] around the object. Commented Jun 9, 2021 at 21:03
  • I added a link to a screenshot of what I have for my array, I just dont know if its possible to generate a random string for a password using whats listed inside the array. Or should I just make each array element (i.e.: specChar) into their own respective arrays and reference them individually. Again im very new to this so I apologize if this isnt making too much sense Commented Jun 9, 2021 at 21:18

1 Answer 1

1

You practically have it, but () doesn't work in JavaScript. You can use an array instead. You can nest arrays, so in your case, arrays inside a dictionary.

var characters = {
    'numbers':["0", "1", "2", "3", "4" , "5", "6" , "7", "8", "9"], 
    'specialChar':["!", "%", "&", ",", "*", "+", "-", ".", "/", "<", ">", "?","~"],
    'upperCase':["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"],
    'lowerCase':["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"]
}

In this code, the dictionary (noted by {}) is holding the arrays with your keys. To utilize this, you can use characters['numbers'][0] to get the first number. In your case, you want random characters, so you can use something like

characters['numbers'][Math.floor(Math.random()*10)]

Keep in mind, however, that dictionaries are not ordered. So if you want to choose randomly between numbers, special characters, and letters, you can choose randomly from an array:

choices = ["numbers", "specialChar", "upperCase", "lowerCase"]
chartype = choices[Math.floor(Math.random() * 4)]

And then use this to get a random character,

characters[chartype][Math.floor(Math.random()*10)]

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! This is exactly what I was thinking of, I haven't learned about dictionaries for arrays yet what an awesome concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.