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.
| Rule | Points |
|---|---|
| Length of at least 8 | 25 |
| Has an uppercase letter | 20 |
| Has a lowercase letter | 20 |
| Has a digit | 20 |
| 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
- Store
lengthaspassword.length. It is used for the score and for the output. - Create the four flags with
let, eachfalse:hasUpper,hasLower,hasDigitandhasSymbol. - Loop over the characters with
for (const c of password). - 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). - Create
scorewithletand start it at 0. - Add the length points:
if (length >= 8)adds 25 withscore += 25. - Add the class points: one
ifeach forhasUpper,hasLowerandhasDigit(20 each), then one forhasSymbol(15). - 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 rule | score |
|---|---|
| length (10 is at least 8) | 25 |
| uppercase | 45 |
| digit | 65 |
| symbol | 80 |
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 iffor 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: 0andScore: 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:
| password | Length line | Score line | Why |
|---|---|---|---|
"Secret!99" | Length: 9 | Score: 100 | all five rules |
"abc123" | Length: 6 | Score: 40 | lowercase and digit |
"abcdefgh" | Length: 8 | Score: 45 | length and lowercase |
"ABCDEFGH1!" | Length: 10 | Score: 80 | length, uppercase, digit and symbol |
"" | Length: 0 | Score: 0 | no rule applies |
