Start learning free

LESSON 200 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V6 in TypeScript: A Score from 0 to 100

Practice Read the full explanation

Combine the earlier checks into a single numeric score between 0 and 100.

You start with:

const password: string = "Secret!99";

Build:

Length: 9
Score: 100

Rules:

  • Start with score = 0.
  • If the length is at least 8, add 25.
  • If the password contains an uppercase letter, add 20.
  • If it contains a lowercase letter, add 20.
  • If it contains a digit, add 20.
  • If it contains a symbol (not a letter and not a digit), add 15.
  • Print "Length: " followed by the length, then print "Score: " followed by the score.

Try other passwords: with abc123 the output is

Length: 6
Score: 40

Run the four character-class detections, then five independent ifs that add to score.

const password: string = "Secret!99";

const length: number = password.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;
    }
}
let score: number = 0;
if (length >= 8) {
    score += 25;
}
if (hasUpper) {
    score += 20;
}
if (hasLower) {
    score += 20;
}
if (hasDigit) {
    score += 20;
}
if (hasSymbol) {
    score += 15;
}
console.log("Length: " + length);
console.log("Score: " + score);

STEP 1 OF 18
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in TypeScript, explained

    Problem overview

    Instead of a Strong or Weak verdict, this version computes a number. The score starts at 0 and gains points for each rule the password meets: 25 for a length of at least 8, 20 for an uppercase letter, 20 for a lowercase letter, 20 for a digit and 15 for a symbol, meaning any character that is not a letter and not a digit. The five amounts add up to 100, so a password that meets every rule scores exactly 100.

    For Secret!99 the program prints:

    Length: 9
    Score: 100

    For abc123 the output is Length: 6 and Score: 40: the length rule fails, and the lowercase letters and the digits give 20 each. The program builds on the flags of the earlier versions and turns them into points.

    Approach: collect the facts, then add up the points

    The work splits into two phases. First, one loop over the characters sets four boolean flags: hasUpper, hasLower, hasDigit and hasSymbol. Second, a list of independent if statements adds points to score, one if per rule.

    Each rule must be its own if. An else if chain would stop at the first rule that is true, so a strong password would earn the points of one rule only. Independent ifs let every satisfied rule add its share.

    Splitting the work like this keeps the scoring easy to change. To make a rule worth more or less, you change one number in one if; the loop that gathers the facts stays the same.

    Here is the scoring phase for abc123, after the loop has set hasLower and hasDigit to true and left the other two flags false:

    ruleconditionpoints addedscore after
    length6 >= 8 is false00
    uppercasehasUpper is false00
    lowercasehasLower is true2020
    digithasDigit is true2040
    symbolhasSymbol is false040

    The final score is 40, which matches the output shown above. Because the points are separate amounts, the order of the five rules doesn't change the result; the order of the two printed lines does.

    Step by step

    1. Store length as a const number from password.length. It is used in the length rule and in the first output line.
    2. Create the four flags hasUpper, hasLower, hasDigit and hasSymbol with let, typed boolean, each false.
    3. Loop with for (const c of password) and close the brace right away.
    4. Inside the loop, set each flag with its own if: /[A-Z]/.test(c) for upper, /[a-z]/.test(c) for lower, /[0-9]/.test(c) for digit, and !/[A-Za-z0-9]/.test(c) for symbol.
    5. Create score with let, typed number, starting at 0.
    6. Add the length points: if length >= 8, then score += 25.
    7. Add the character-class points with one if each: 20 for hasUpper, 20 for hasLower, 20 for hasDigit, and 15 for hasSymbol.
    8. Print the two lines: the length first, then the score.

    The solution in TypeScript

    const password: string = "Secret!99";
    
    const length: number = password.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;
        }
    }
    let score: number = 0;
    if (length >= 8) {
        score += 25;
    }
    if (hasUpper) {
        score += 20;
    }
    if (hasLower) {
        score += 20;
    }
    if (hasDigit) {
        score += 20;
    }
    if (hasSymbol) {
        score += 15;
    }
    console.log("Length: " + length);
    console.log("Score: " + score);

    Details that matter:

    • score += 25 is short for score = score + 25. Because score changes, it needs let.
    • A flag can be used as a condition directly. if (hasUpper) is enough, and if (hasUpper === true) would mean the same thing.
    • The regular expressions cover ASCII letters only. A character like é is not a letter for [a-z], so it counts toward the symbol rule instead.
    • The scoring ifs run after the loop. The loop must finish first, because a flag isn't final until every character has been tested.

    One more property is worth noting: the score can only be one of a few values, because it is a sum of a subset of 25, 20, 20, 20 and 15. It never goes below 0 or above 100, since the points are only ever added and the five amounts total 100. A tool that shows the score can rely on that range without clamping it.

    Complexity

    The loop makes one pass over the n characters with constant work per character, so the time is O(n). The five scoring ifs are constant time, and the space is O(1).

    Edge cases and common mistakes

    • Using else if for the scoring rules. The password Secret!99 would then stop at 25 instead of reaching 100.
    • Counting every matching character. A rule gives its points once, however many uppercase letters there are. That is why flags, not counters, are used.
    • An empty password. No flag is set and the length rule fails, so the output is Length: 0 and Score: 0.
    • A short password with every class. Ab1! has an uppercase letter, a lowercase letter, a digit and a symbol, but only 4 characters. It scores 20 + 20 + 20 + 15 = 75, so the length rule is the only one missing.
    • A long password with one class. ABCDEFGH earns 25 for the length and 20 for the uppercase letters: 45 in total.
    • Printing before the score is final. The score line must come after all five rules, so print at the end.

    Testing

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

    passwordLength lineScore line
    Secret!99Length: 9Score: 100
    abc123Length: 6Score: 40
    Ab1!Length: 4Score: 75
    ABCDEFGHLength: 8Score: 45
    empty stringLength: 0Score: 0
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free