Start learning free

LESSON 194 OF 285 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V4 in JavaScript: Count Each Character Class

Practice Read the full explanation

Count how many characters of each class the password contains.

You start with:

const password = "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 = "Hello!World9";

let uppercase = 0;
let lowercase = 0;
let digits = 0;
let symbols = 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

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V4 in JavaScript, explained

    Problem overview

    The previous version asked whether a password has each kind of character. This version asks how many. The program counts the uppercase letters, the lowercase letters, the digits and the symbols, where a symbol is any character that is not a letter and not a digit, and prints four lines.

    For "Hello!World9" the output is:

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

    For "Pass word!2", where the space and the exclamation mark are both symbols, it is:

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

    Approach: four counters instead of four flags

    A boolean flag can only say "seen" or "not seen". A number can say how many. So the four flags become four integer counters, each starting at 0. The loop visits each character once, and when a character belongs to a class, it adds 1 to that class's counter with ++.

    Each class has its own if inside the loop, with the same four patterns as before. The program has no verdict this time; it only reports the counts, after the loop has finished.

    Counting gives the program more information than flags did. A flag can't tell a password with one digit from a password with ten, but a counter can. This version only reports the counts; it makes no decision about them.

    Step by step

    1. Create four counters with let, one at a time: uppercase, lowercase, digits and symbols, each set to 0. They use let because they change while the loop runs.
    2. Start a for...of loop over password. It runs once per character: 12 times for "Hello!World9".
    3. Uppercase: if (/[A-Z]/.test(c)) adds 1 to uppercase.
    4. Lowercase: /[a-z]/ adds 1 to lowercase.
    5. Digit: /[0-9]/ adds 1 to digits.
    6. Symbol: !/[A-Za-z0-9]/.test(c) adds 1 to symbols. Letters and digits make this false, and anything else, such as ! or a space, makes it true.
    7. Print the four lines after the loop, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined to a number with +.

    The solution in JavaScript

    const password = "Hello!World9";
    
    let uppercase = 0;
    let lowercase = 0;
    let digits = 0;
    let symbols = 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);

    Following the counters through the loop

    These are the four counters, in the order uppercase, lowercase, digits, symbols, after each character of "Hello!World9":

    CharacterCounters afterwards
    H1, 0, 0, 0
    e1, 1, 0, 0
    l1, 2, 0, 0
    l1, 3, 0, 0
    o1, 4, 0, 0
    !1, 4, 0, 1
    W2, 4, 0, 1
    o2, 5, 0, 1
    r2, 6, 0, 1
    l2, 7, 0, 1
    d2, 8, 0, 1
    92, 8, 1, 1

    Every character raises exactly one counter by one. The last row, 2, 8, 1, 1, is what the four console.log calls print. If you ever see counters that don't add up to the length, a test is wrong or a character class is missing.

    Details worth knowing

    uppercase++ is shorthand for uppercase = uppercase + 1. Used as a statement on its own line, it simply adds one.

    The four tests are independent ifs. For any character, at most one of them is true, so the four counters add up to the number of characters the loop visited. For "Hello!World9" that is 2 + 8 + 1 + 1 = 12.

    A for...of loop over a string goes through code points, while .length counts UTF-16 code units. For "😀a", .length is 3, but the loop sees two characters. The emoji is neither a letter nor a digit, so the program prints Symbols: 1 and Lowercase: 1. Likewise "héllo" prints Lowercase: 4 and Symbols: 1, because é is outside the ASCII range a-z and so counts as a symbol under the lesson's rule.

    The numbers join the label as text through +. The counter itself stays a number, which is why uppercase++ keeps working after the loop. If a counter were a string, ++ would convert it to a number first.

    Flags and counters side by side

    Compared with the last version, three things change. The starting values are 0 instead of false. The update is uppercase++ instead of hasUpper = true. And the output prints a number, so no ternary is needed to pick True or False. The loop and the four patterns are identical, which is what makes it easy to move from one version to the other.

    Complexity

    The loop runs once per character and does four constant-time tests each time, so the time is O(n) for a password of length n. The program stores four numbers, so the extra space is O(1).

    Edge cases and common mistakes

    • Declaring a counter with const. uppercase++ would throw a TypeError.
    • Resetting a counter inside the loop. The count would restart on every character.
    • Printing inside the loop. You would get four lines for each character.
    • Forgetting that the space is a symbol. In "Pass word!2", both the space and ! count.
    • Starting a counter at 1. Every count would be one too high, and a password with no digits would report Digits: 1.
    • The empty string. The loop doesn't run, and all four counts are 0.
    • The wrong order of the output lines. Keep Uppercase, Lowercase, Digits, Symbols.

    Testing

    Try these in the editor above:

    passwordUppercaseLowercaseDigitsSymbols
    "Hello!World9"2811
    "Pass word!2"1712
    ""0000
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free