Start learning free

LESSON 197 OF 671 · PROJECTS: EVERYDAY PROGRAMS

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

const length = password.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;
    }
}
let score = 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

    JAVASCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V6 in JavaScript, explained

    Problem overview

    This version turns the earlier yes/no checks into a number. Each rule that the password meets adds points to a score, and the program prints the length and the final score.

    RulePoints
    Length of at least 825
    Has an uppercase letter20
    Has a lowercase letter20
    Has a digit20
    Has a symbol (not a letter and not a digit)15

    The points add up to 25 + 20 + 20 + 20 + 15 = 100. For "Secret!99" every rule is met, and the output is:

    Length: 9
    Score: 100

    For "abc123" only the lowercase and digit rules are met, which is 40 points:

    Length: 6
    Score: 40

    Approach: detect first, score second

    The program works in two stages. First, the same loop as in the earlier versions sets four flags, hasUpper, hasLower, hasDigit and hasSymbol. Then a series of if statements turns the facts into points. Keeping the two stages apart makes each easy to follow: the loop only answers "what is in the password?", and the scoring only answers "what is that worth?".

    Each scoring rule is its own if, never an else if. The rules are independent and all of them can apply at once, so every one must be tested. With an else if chain, a password with an uppercase letter would skip the checks below it.

    Step by step

    1. Store length as password.length. It is used for the score and for the output.
    2. Create the four flags with let, each false: hasUpper, hasLower, hasDigit and hasSymbol.
    3. Loop over the characters with for (const c of password).
    4. Set the flags inside the loop, with four separate ifs: /[A-Z]/.test(c), /[a-z]/.test(c), /[0-9]/.test(c) and !/[A-Za-z0-9]/.test(c).
    5. Create score with let and start it at 0.
    6. Add the length points: if (length >= 8) adds 25 with score += 25.
    7. Add the class points: one if each for hasUpper, hasLower and hasDigit (20 each), then one for hasSymbol (15).
    8. Print the two lines after all the points are added: the length first, then the score.

    The solution in JavaScript

    const password = "Secret!99";
    
    const length = password.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;
        }
    }
    let score = 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);

    Following the score

    For "abc123" the flags after the loop are: no uppercase, lowercase yes, digit yes, no symbol. The length is 6, so the length rule adds nothing. The score then goes from 0 to 20 at the lowercase rule and to 40 at the digit rule. The other two rules don't apply, and the program prints Score: 40.

    For "ABCDEFGH1!" the length rule applies first, then three of the class rules, with no lowercase letters:

    After the rulescore
    length (10 is at least 8)25
    uppercase45
    digit65
    symbol80

    The lowercase rule is skipped because hasLower is still false. The final score is 80, which is 100 minus the 20 points for the missing lowercase letter.

    Details worth knowing

    score += 25 is shorthand for score = score + 25. The operator works the same way for -=, *= and so on.

    The if (hasUpper) tests need no === true, because the flags are booleans already.

    The weights are chosen so that the total is exactly 100 when everything is present. A password can't score more than 100, and none of the rules can subtract points, so the score never goes below 0.

    The flags are only a convenient middle step here. You could add the points directly inside the loop, but then a second uppercase letter would add 20 again. The flags make each rule count once, however many matching characters there are.

    Changing the weights

    Because every rule is a separate if with its own number, changing the policy is a one-line edit. To make a symbol worth 25 points, change the 15 in its if. The score then can reach 110, so the weights must be kept in balance: if you want a maximum of 100, another rule has to lose 10 points. Nothing in the code enforces the maximum. It holds because the weights are chosen to add up to 100.

    Complexity

    The loop is O(n) for a password of n characters, and the scoring is a fixed number of steps. The time is O(n) and the extra space is O(1).

    Edge cases and common mistakes

    • Using else if for the scoring. Only the first matching rule would add points, so "Secret!99" would score far less than 100.
    • Adding points inside the loop. Every matching character would add the points again.
    • Using > for the length rule. A password of exactly 8 characters would lose its 25 points.
    • Counting a rule twice. The flags are booleans for this reason: a password with five uppercase letters still earns 20 points for the uppercase rule once.
    • The empty password. No rule applies, so the output is Length: 0 and Score: 0.
    • Printing before the score is final. The two lines come last.

    Testing

    Try these in the editor above. Each score is the sum of the rules that apply:

    passwordLength lineScore lineWhy
    "Secret!99"Length: 9Score: 100all five rules
    "abc123"Length: 6Score: 40lowercase and digit
    "abcdefgh"Length: 8Score: 45length and lowercase
    "ABCDEFGH1!"Length: 10Score: 80length, uppercase, digit and symbol
    ""Length: 0Score: 0no rule applies
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free