New to programming here, and I am just about to finish my first project-a password generator. I am trying to keep it as simple as possible, nothing fancy, yet I have come to a standstill. I want to implement an option that allows the user to only get one character to appear only once in the generated password. As of now, it is just a random jumble of characters, repeating and whatnot, so I was wondering if there is any way to implement such a feature-and if so, how? If statements? loops? I am up for all suggestions!
Here is the code.
var keys = {
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","z"],
number: ["0","1","2","3","5","6","7","8","9"],
symbol: ["!","@","#","$","%","^","&","*","(",")","_"," ","~","|","}","{","[","]",":",";","?",">","<",",",".","/","-","="]
}
var getKey = [
function upperCase() {
return keys.upperCase[Math.floor(Math.random() * keys.upperCase.length)];
},
function lowerCase() {
return keys.lowerCase[Math.floor(Math.random() * keys.lowerCase.length)];
},
function number() {
return keys.number[Math.floor(Math.random() * keys.number.length)];
},
function symbol() {
return keys.symbol[Math.floor(Math.random() * keys.symbol.length)];
}
];
function createPassword() {
var upper = document.getElementById("upperCase").checked;
var lower = document.getElementById("lowerCase").checked;
var number = document.getElementById("number").checked;
var symbol = document.getElementById("symbol").checked;
if (upper lower number symbol === 0) {
alert("Please check a box!");
return;
}
var passwordBox = document.getElementById("passwordBox");
var length = document.getElementById("length");
var password = "";
while (length.value > password.length) {
var keyToAdd = getKey[Math.floor(Math.random() * getKey.length)];
var isChecked = document.getElementById(keyToAdd.name).checked;
if (isChecked) {
password = keyToAdd();
}
}
passwordBox.innerHTML = password;
}
CodePudding user response:
@Embla I think this is what you are trying to achieve, let me know if smthg is missing in this solution. You didnt provide HTML, I just assumed what it might look like
let passwordBox = document.getElementById("passwordBox");
let length = document.getElementById("length");
const keys = {
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","z"],
number: ["0","1","2","3","5","6","7","8","9"],
symbol: ["!","@","#","$","%","^","&","*","(",")","_"," ","~","|","}","{","[","]",":",";","?",">","<",",",".","/","-","="]
}
function random( max, min=0){
return ~~(Math.random() * (max - min) min)
}
function getKey(objArr){
const values = Object.values(objArr);
const randValue = values[random(values.length)];
return randValue[random(randValue.length)];
}
function createPassWord(passwordLength){
const pwdStorage = new Set();
while( pwdStorage.size < passwordLength ){
pwdStorage.add(getKey(keys))
}
return [...pwdStorage].join('');
}
length.addEventListener('change', (event)=>{
passwordBox.textContent = createPassWord( length.value)
})
input[type="number"]{
width: 150px;
height: 30px;
border: 2px solid grey;
border-radius: 5px;
padding-left:10px;
font-size: 18px;
}
<div >
<h2 id="passwordBox">Password</h2>
</div>
<input type="number" name="" id="length" min="1" max="100" step="1" placeholder="Enter number">
CodePudding user response:
I writed a function for your request, i hope that works for you.
Array.prototype.getAndRemove = function (index) {
const item = this[index];
if (this.indexOf(item) > -1) {
this = this.splice(this.indexOf(item), 1);
if (this.indexOf(item) > -1) {
this = this.splice(this.indexOf(item), 1);
}
}
return item;
}