Search code examples
javascriptarrayshashmap

check whether new array contains the special character using hashmap


New to JavaScript Programming. So I will make this short for you to read so you dont waste your time.

Trying to get a check to work for a password generator.

const chars = ["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,"!", "@", "#", "$", "%" ]
const specialChars = ["!", "@", "#", "$", "%"]
const nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const genEl = document.getElementById("generate-el")
const inputEl = document.getElementById("input-el")
genEl.addEventListener("click", generate);
let pass = [];

let randSpecialChars = specialChars[Math.floor(Math.random() * specialChars.length)]
let randNums = nums[Math.floor(Math.random() * nums.length)]

const table ={};
table['randSpecialChars'] = randSpecialChars;
table['randNums'] = randNums;

function generate(){
    for(let i=0; i<10 && i<chars.length; i++){
        pass[i] = chars[Math.floor(Math.random() * chars.length)];
        inputEl.value = pass.join('');
};

    for(let i=0; i<pass.length; i++){
        if(specialChars.includes(pass[i])){
            return
        }
        else if(nums.includes(pass[i])){
            return
        }
        else{
            if(pass.includes(table['randSpecialChars'])=== false){
            pass.pop();
            pass.push(table['randSpecialChars'])}
            else if(pass.includes(table['randNums']==false)){
            pass.pop();
            pass.push(table['randSpecialChars'])
            }
            
        }
    }
};

My logic: If the generated password does not contain the special character pop the last element from the array and then push the random Special Character from the hashmap.

Same for the randNum it gets a random number from the array stores it in a variable, the number is then stored in the hashmap AS the value.

Any help and improvement would be appreciated. Thanks !


Solution

  • Maybe this regex will work for you:

    /[ `!@#$%^&*()_+-=[]{};':"\|,.<>/?~]/

    function checkForSpecialChars(password) {
        return /[ `!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~]/.test(password);
    }
    <input type="text" oninput="checkForSpecialChars(this.value) ? console.log('yup') : console.log('nope')">

    Now, if you have a text and it contains a regex, you might want to replace a character in it, but why the last one? Why not any of it?

    let specialCharacter = "."; //use yours instead, this is just a test
    
    let context = document.getElementById("foo");
    
    let value = foo.value;
    
    let position = Math.random() * value.length;
    
    foo.value = value.substring(0, position - 1) + specialCharacter + value.substring(position);
    <input type="text" value="123456789" id="foo">

    Combine the two and your problem will be solved.