Start learning free

LESSON 193 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V3 in JavaScript: 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 = "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 = "Secret!99";

const length = password.length;
console.log("Length: " + length);
let hasUpper = false;
let hasLower = false;
let hasDigit = false;
let hasSymbol = 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

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V3 in JavaScript, explained

    Problem overview

    This version checks 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 for each kind, and a verdict. The password is Strong only if it has at least 8 characters and all four kinds.

    For "Secret!99" the output is:

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

    For "secret99", which has no capital and no symbol, it is:

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

    Approach: several flags, one pass

    The previous version used one flag, hasDigit. This one uses four flags, hasUpper, hasLower, hasDigit and hasSymbol, each starting as false. A single loop visits every character once, and each character can switch on the flag that matches it. There is no need to loop four times.

    Inside the loop, the four tests are four separate if statements, not an if/else if chain. A chain would stop at the first match, which is fine for exclusive categories but means each test would depend on the ones above it. Four independent ifs are easier to read: each says what it checks and what it sets.

    The symbol test is defined as the complement of the letters and digits, not as a list of allowed punctuation. That means no character is left without a class: every character is a letter, a digit or a symbol. It also means a space, a dash or an accented letter counts as a symbol without any extra code.

    Step by step

    1. Store and print the length, as in the earlier versions.
    2. Create hasUpper and hasLower with let, both false.
    3. Create hasDigit and hasSymbol the same way.
    4. Start a for...of loop over password, with const c as the current character.
    5. Uppercase test: /[A-Z]/.test(c) sets hasUpper.
    6. Lowercase test: /[a-z]/.test(c) sets hasLower, in its own if.
    7. Digit test: /[0-9]/.test(c) sets hasDigit.
    8. Symbol test: !/[A-Za-z0-9]/.test(c) sets hasSymbol. The pattern matches letters and digits, and the ! in front turns true into false and back, so the test is true for anything else.
    9. Print the four flag lines after the loop, in the order Upper, Lower, Digit, Symbol, each as True or False with the ternary.
    10. Write the verdict. Five conditions joined with &&: the length and the four flags. Print Strong if all hold.
    11. Add the else that prints Weak.

    The solution in JavaScript

    const password = "Secret!99";
    
    const length = password.length;
    console.log("Length: " + length);
    let hasUpper = false;
    let hasLower = false;
    let hasDigit = false;
    let hasSymbol = 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");
    }

    Following the flags through the loop

    This table shows the four flags, in the order upper, lower, digit, symbol, with 1 for true and 0 for false, after each character of "Ab1!":

    CharacterFlags afterwards
    A1000
    b1100
    11110
    !1111

    Each character switches on exactly one flag and leaves the other three alone. Flags that are already on stay on, because nothing in the loop ever sets a flag to false. After the last character all four are on, so the four conditions in the verdict pass. The fifth condition, the length, fails for this short password, which is why "Ab1!" is still Weak.

    Details worth knowing

    The ranges A-Z, a-z and 0-9 are ASCII ranges. A letter such as é is outside them, so /[A-Za-z0-9]/.test("é") is false, and the symbol test counts é as a symbol. That follows the lesson's definition, which is "not a letter and not a digit" in the ASCII sense. A broader check would need Unicode-aware patterns.

    The ! operator negates a boolean, and it applies to the whole call /[A-Za-z0-9]/.test(c). It reads as "it is not true that c is a letter or digit".

    A flag line such as "Upper: " + (hasUpper ? "True" : "False") needs its parentheses. Without them, + runs first, and the condition would be the non-empty text "Upper: true" instead of the flag.

    The verdict repeats && five times. It short-circuits: as soon as one operand is false, the rest are not evaluated. Here that makes no difference to the result, only to how much work is done.

    Edge cases and common mistakes

    • Using else if between the four tests. It would still work for these four categories, because a character belongs to only one, but it hides the independence of the tests and breaks if you later add a class that overlaps another.
    • Forgetting the length rule. "Ab1!" has all four kinds, but only 4 characters, so it is Weak.
    • Missing one kind. "Secret99" has 8 characters and three kinds. It has no symbol, so it is Weak.
    • Treating a space as a letter. Spaces count as symbols here.
    • Putting the flag lines inside the loop. They would print once per character.

    Testing

    Try these in the editor above. In each case only the Strong password has all five conditions.

    passwordUpperLowerDigitSymbolVerdict
    "Secret!99"TrueTrueTrueTrueStrong
    "secret99"FalseTrueTrueFalseWeak
    "Secret99"TrueTrueTrueFalseWeak
    "SECRET!99"TrueFalseTrueTrueWeak
    "Ab1!"TrueTrueTrueTrueWeak
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free