Strong Password

  • + 0 comments

    JavaScript

    function minimumNumber(n, password) {
        // Return the minimum number of characters to make the password strong
        const NUM = "0123456789";
        const LOW = "abcdefghijklmnopqrstuvwxyz";
        const UPP = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        const SPE = "!@#$%^&*()-+";
        const TYPES = [NUM, LOW, UPP, SPE];
        const MIN_LENGTH = 6;
        let typesIncluded = 0;
        let hasNum = false, hasLow = false, hasUpp = false, hasSpe = false;
        let result = 0;
    
        for(let item of password){
            if(NUM.indexOf(item) !== -1 && !hasNum){
                hasNum = true;
                typesIncluded++;
                continue;
            }
    
            if(LOW.indexOf(item) !== -1 && !hasLow){
                hasLow = true;
                typesIncluded++;
                continue;
            }
    
            if(UPP.indexOf(item) !== -1 && !hasUpp){
                hasUpp = true;
                typesIncluded++;
                continue;
            }
    
            if(SPE.indexOf(item) !== -1 && !hasSpe){
                hasSpe = true;
                typesIncluded++;
                continue;
            }
        }
        
        result = Math.max(MIN_LENGTH - password.length, TYPES.length - typesIncluded);
        return result;
    }