Strong Password

  • + 0 comments

    My answer in Typescript, simple, not minimized

    function minimumNumber(n: number, password: string): number {
        /**
         * simple
         * 1. check [password] missing [x] in length
         * 2. check [password] missing [y] in types of character
         * 3. return the larger [x] or [y]
         * 
         * why? 
         * if you miss 3 length but miss 1 type, you can write that missing type
         * in 3 character missing length to satisfy both conditions. 
         * if tou miss 1 length but miss 3 type, you need to type at least 3 character
         * in 3 type and that satisfy missing length condition too.
         */
        const check_miss_length = (): number => {
            if (n < 6) return 6 - n
            return 0
        }
        const check_miss_types = (): number => {
            const type_digit = (/([\d])/.test(password)) ? 0 : 1
            const type_lower = (/([a-z])/.test(password)) ? 0 : 1
            const type_upper = (/([A-Z])/.test(password)) ? 0 : 1
            const type_speci = (/([^a-z0-9A-Z])/.test(password)) ? 0 : 1
            return type_digit + type_lower + type_upper + type_speci;
        }
    
        return Math.max(check_miss_length(), check_miss_types())
    }