Start learning free

LESSON 198 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in TypeScript: Counting Character Classes

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

const password: string = "Hello!World9";

Build:

Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1

Rules:

  • Use four integer counters, all starting at 0.
  • Loop through each character and add 1 to the matching counter.
  • Treat any character that is not a letter and not a digit as a symbol.
  • Print one line per counter, in this order: "Uppercase: ", "Lowercase: ", "Digits: ", "Symbols: ", each followed by the count.

Try other passwords: with Pass word!2 the output is

Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2

Use uppercase++, etc. to increment integer counters.

const password: string = "Hello!World9";

let uppercase: number = 0;
let lowercase: number = 0;
let digits: number = 0;
let symbols: number = 0;
for (const c of password) {
    if (/[A-Z]/.test(c)) {
        uppercase++;
    }
    if (/[a-z]/.test(c)) {
        lowercase++;
    }
    if (/[0-9]/.test(c)) {
        digits++;
    }
    if (!/[A-Za-z0-9]/.test(c)) {
        symbols++;
    }
}
console.log("Uppercase: " + uppercase);
console.log("Lowercase: " + lowercase);
console.log("Digits: " + digits);
console.log("Symbols: " + symbols);

STEP 1 OF 13
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in TypeScript, explained

    Problem overview

    This version stops asking "is there at least one?" and asks "how many?". For the password Hello!World9 the program prints four counters, one per kind of character:

    Uppercase: 2
    Lowercase: 8
    Digits: 1
    Symbols: 1

    The uppercase letters are H and W, the digit is 9, the symbol is !, and the other eight characters are lowercase. Any character that is not a letter and not a digit counts as a symbol, and that includes a space: for Pass word!2 the output is Uppercase: 1, Lowercase: 7, Digits: 1 and Symbols: 2, with the space and the ! as the two symbols. The previous version used booleans to remember that a class had been seen; this one uses integers to remember how often.

    Approach: four counters and one pass

    A counter is a variable that starts at 0 and grows by one each time something is found. The program needs four of them, one for each class. A single loop over the characters tests each character against the four classes, and each matching test adds one to its counter.

    The classes are defined with regular expressions. /[A-Z]/ matches one capital letter, /[a-z]/ one small letter, and /[0-9]/ one digit. The symbol test is the complement: !/[A-Za-z0-9]/.test(c) is true when the character is neither a letter nor a digit, so no list of symbols is needed.

    Every character lands in exactly one class, which gives a handy self-check: the four counters always add up to the length of the password. For Hello!World9, 2 + 8 + 1 + 1 is 12, and the string has 12 characters.

    Why four separate variables and not a table? With a fixed, small set of classes, named counters are the clearest choice: the output lines read straight from them. A table, such as an object keyed by class name, pays off when the set of keys isn't known beforehand. Here it is known, so plain variables keep the code short and every name tells you what it holds.

    Step by step

    1. Create uppercase with let, typed number, set to 0.
    2. Create lowercase the same way.
    3. Create digits the same way.
    4. Create symbols the same way. The counters use let because their values change while the loop runs.
    5. Open for (const c of password) and close its brace right away. The loop runs once per character, 12 times for the sample password.
    6. Uppercase test. If /[A-Z]/.test(c), do uppercase++.
    7. Lowercase test. If /[a-z]/.test(c), do lowercase++.
    8. Digit test. If /[0-9]/.test(c), do digits++.
    9. Symbol test. If !/[A-Za-z0-9]/.test(c), do symbols++.
    10. After the loop, print four lines, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined to a counter with +, which turns the number into text.

    The solution in TypeScript

    const password: string = "Hello!World9";
    
    let uppercase: number = 0;
    let lowercase: number = 0;
    let digits: number = 0;
    let symbols: number = 0;
    for (const c of password) {
        if (/[A-Z]/.test(c)) {
            uppercase++;
        }
        if (/[a-z]/.test(c)) {
            lowercase++;
        }
        if (/[0-9]/.test(c)) {
            digits++;
        }
        if (!/[A-Za-z0-9]/.test(c)) {
            symbols++;
        }
    }
    console.log("Uppercase: " + uppercase);
    console.log("Lowercase: " + lowercase);
    console.log("Digits: " + digits);
    console.log("Symbols: " + symbols);

    Details worth knowing:

    • x++ adds 1 to x; it is short for x = x + 1. It changes the variable, which is why the counters are declared with let and not const.
    • All counts are number. TypeScript has one number type for integers and decimals alike. Counts this small are exact, and "Digits: " + digits prints a plain 1.
    • The regular expressions cover ASCII letters only. Ü and ï are not in A-Z or a-z, so they are counted as symbols. For the password Ünï9 the output is Uppercase: 0, Lowercase: 1, Digits: 1 and Symbols: 2.
    • The type annotations are erased when the file is compiled to JavaScript and don't change how the program runs.

    The order of the four tests inside the loop doesn't affect the result, because each test only touches its own counter. The order of the four console.log lines does matter: the output must list Uppercase, Lowercase, Digits and then Symbols.

    Complexity

    The loop makes one pass over the n characters with four constant-time tests per character, so the time is O(n). The space is O(1): four counters, however long the password.

    Edge cases and common mistakes

    • An empty password. The loop never runs and all four lines show 0.
    • Leaving out one class's test. Each class needs a test that runs for every character. Without it, that counter stays at 0 and the four counters no longer add up to the length.
    • Declaring the counters with const. uppercase++ would then be an error, because a constant can't be changed.
    • Starting a counter without a value. A let counter that is never set holds undefined, and undefined++ gives NaN, so every counter must start at 0.
    • Forgetting the space. A space is a symbol, so passwords with spaces have a higher symbol count than the visible punctuation suggests.
    • Printing inside the loop. The four lines must appear once, after the loop.

    Testing

    Change password in the editor above. These results come from the reference solution:

    passwordUppercaseLowercaseDigitsSymbols
    Hello!World92811
    Pass word!21712
    empty string0000
    Ünï90112
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free