Start learning free

LESSON 197 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in TypeScript: Four Character-Class Flags

Practice Read the full explanation

Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?

You start with:

const password: string = "Secret!99";

Build:

Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong

Rules:

  • Print "Length: " followed by the number of characters.
  • Start four flags (hasUpper, hasLower, hasDigit, hasSymbol) at false. Loop over every character and set the matching flag to true.
  • A symbol is any character that is not a letter and not a digit.
  • Print one line per flag, in this order: "Upper: ", "Lower: ", "Digit: ", "Symbol: ", each followed by the flag. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If the length is at least 8 and all four flags are true (stricter than the last lesson), print Strong; otherwise print Weak.

Try other passwords: with secret99 the output is

Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak

Use regex tests like /[A-Z]/.test(c) for each class. Symbol = !/[A-Za-z0-9]/.test(c).

const password: string = "Secret!99";

const length: number = password.length;
console.log("Length: " + length);
let hasUpper: boolean = false;
let hasLower: boolean = false;
let hasDigit: boolean = false;
let hasSymbol: boolean = false;
for (const c of password) {
    if (/[A-Z]/.test(c)) {
        hasUpper = true;
    }
    if (/[a-z]/.test(c)) {
        hasLower = true;
    }
    if (/[0-9]/.test(c)) {
        hasDigit = true;
    }
    if (!/[A-Za-z0-9]/.test(c)) {
        hasSymbol = true;
    }
}
console.log("Upper: " + (hasUpper ? "True" : "False"));
console.log("Lower: " + (hasLower ? "True" : "False"));
console.log("Digit: " + (hasDigit ? "True" : "False"));
console.log("Symbol: " + (hasSymbol ? "True" : "False"));
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
    console.log("Strong");
} else {
    console.log("Weak");
}

STEP 1 OF 13
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in TypeScript, explained

    Problem overview

    The checker now looks at four kinds of character: uppercase letters, lowercase letters, digits and symbols. A symbol is any character that is neither a letter nor a digit. The program prints the length, one line per kind with True or False, and then a verdict. The password is now Secret!99:

    Length: 9
    Upper: True
    Lower: True
    Digit: True
    Symbol: True
    Strong

    The verdict is stricter than in the previous version. It needs at least 8 characters and all four flags true; any miss gives Weak. For secret99 the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak.

    Approach: one loop, four independent flags

    The previous version used one flag for one question. Here the same pattern is repeated four times: each flag starts at false, and a loop over the characters flips it when it sees a matching character. One pass over the string is enough to update all four flags.

    Inside the loop the four tests are four separate if statements, not an if / else if chain. The four classes never overlap for a single character, so a chain would give the same result here. Separate ifs are still the safer shape: each rule stands alone, and adding or changing one rule can't break another.

    On Secret!99, the S flips hasUpper. The letters e, c, r, e, t flip hasLower. The ! flips hasSymbol and the two 9s flip hasDigit. After nine passes all four flags are true, and the length is 9, so the verdict is Strong.

    The symbol test is defined by exclusion. The pattern /[A-Za-z0-9]/ matches a letter or a digit, so putting ! in front of the test gives "not a letter and not a digit". That avoids listing every possible symbol character.

    Step by step

    1. Store length and print the first line.
    2. Create hasUpper and hasLower with let, typed boolean, both false.
    3. Create hasDigit and hasSymbol the same way.
    4. Open a for (const c of password) loop and close its brace at once.
    5. Uppercase test. If /[A-Z]/.test(c), set hasUpper to true.
    6. Lowercase test. If /[a-z]/.test(c), set hasLower to true.
    7. Digit test. If /[0-9]/.test(c), set hasDigit to true.
    8. Symbol test. If !/[A-Za-z0-9]/.test(c), set hasSymbol to true.
    9. After the loop, print the four flag lines in the order Upper, Lower, Digit, Symbol, each with a ternary that turns the boolean into True or False.
    10. Write the verdict. One if joins five conditions with &&: the length and the four flags. The else prints Weak.

    The solution in TypeScript

    const password: string = "Secret!99";
    
    const length: number = password.length;
    console.log("Length: " + length);
    let hasUpper: boolean = false;
    let hasLower: boolean = false;
    let hasDigit: boolean = false;
    let hasSymbol: boolean = false;
    for (const c of password) {
        if (/[A-Z]/.test(c)) {
            hasUpper = true;
        }
        if (/[a-z]/.test(c)) {
            hasLower = true;
        }
        if (/[0-9]/.test(c)) {
            hasDigit = true;
        }
        if (!/[A-Za-z0-9]/.test(c)) {
            hasSymbol = true;
        }
    }
    console.log("Upper: " + (hasUpper ? "True" : "False"));
    console.log("Lower: " + (hasLower ? "True" : "False"));
    console.log("Digit: " + (hasDigit ? "True" : "False"));
    console.log("Symbol: " + (hasSymbol ? "True" : "False"));
    if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
        console.log("Strong");
    } else {
        console.log("Weak");
    }

    Language details to know:

    • for...of on a string walks it one character at a time. Characters outside the basic range, such as emoji, come out as one item each; that matters little here, because they count as symbols either way.
    • !/[A-Za-z0-9]/.test(c) reads as "not (the test)". The ! applies to the result of .test(c), which is a boolean.
    • The ranges are ASCII only. A letter such as é matches none of [A-Z], [a-z] or [0-9], so it counts as a symbol. With the password Secret99é the output is Symbol: True and the verdict is Strong, even though there is no punctuation.
    • The parentheses around each ternary are needed so the word is chosen before it is joined to the label.

    It is worth seeing why the flags are computed before anything is printed. The first flag line must show a fact about the whole password, and the loop has not finished until the last character has been tested. Printing inside the loop would produce nine copies of each line for a nine-character password. Computing first, printing second, and deciding last gives three clean phases: collect the facts, report them, judge them.

    The verdict also shows how && chains: length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol is true only when every part is true, and evaluation stops at the first false part. The flags are plain booleans, so they can be used as conditions directly; writing hasUpper === true adds nothing.

    Complexity

    There is 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 booleans and a few other variables.

    Edge cases and common mistakes

    • A space is a symbol. It is not a letter or a digit, so Secret 99 prints Symbol: True.
    • Forgetting the length rule. Abc1! has all four classes, but it has only 5 characters, so the output ends with Weak.
    • Omitting the ! in the symbol test. Then hasSymbol would mean "has a letter or digit" and nearly every password would show Symbol: True.
    • Printing the flags in the wrong order. The lines must follow Upper, Lower, Digit, Symbol.

    Testing

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

    passwordUpperLowerDigitSymbolVerdict
    Secret!99TrueTrueTrueTrueStrong
    secret99FalseTrueTrueFalseWeak
    Secret 99TrueTrueTrueTrueStrong
    Abc1!TrueTrueTrueTrueWeak
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free