mardi 7 septembre 2021

Ruby Array Elements

I am trying to create password Generate in ruby. At the moment all is working just got stuck at the final piece of generating the password.

I asked user if he/she would like the password to include numbers, lowercase or uppercase. If YES, user will enter 1 and 0 for NO.

I used the code below to generate password if everything is 1. Meaning user want to include numbers, lowercase and uppercase.

 if numbers == 1 && lowercase == 1 && uppercase == 1
     passGen = [(0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a].flatten.sample(10)
 end

 p passGen

This works 90% of the time. 10% of the time the generated password will not include say any numbers. But everything else present. I am not sure if this is because of the size or length of Array from which the password is sampled.

Anyway lets go to the main problem below

Here is the problem, I am struggling to write the code to generate password if one or more of input is 0. That's if user don't want to include numbers. Or no numbers and uppercase etc . As I can't predict what user may want or not want. I need help on this please. Thank you.

Aucun commentaire:

Enregistrer un commentaire