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:
| rule | condition | points added | score after |
|---|---|---|---|
| length | 6 >= 8 is false | 0 | 0 |
| uppercase | hasUpper is false | 0 | 0 |
| lowercase | hasLower is true | 20 | 20 |
| digit | hasDigit is true | 20 | 40 |
| symbol | hasSymbol is false | 0 | 40 |
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
- Store
lengthas aconstnumber frompassword.length. It is used in the length rule and in the first output line. - Create the four flags
hasUpper,hasLower,hasDigitandhasSymbolwithlet, typedboolean, eachfalse. - Loop with
for (const c of password)and close the brace right away. - 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. - Create
scorewithlet, typednumber, starting at 0. - Add the length points: if
length >= 8, thenscore += 25. - Add the character-class points with one
ifeach: 20 forhasUpper, 20 forhasLower, 20 forhasDigit, and 15 forhasSymbol. - 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 += 25is short forscore = score + 25. Becausescorechanges, it needslet.- A flag can be used as a condition directly.
if (hasUpper)is enough, andif (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 iffor the scoring rules. The passwordSecret!99would 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: 0andScore: 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.
ABCDEFGHearns 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:
| password | Length line | Score line |
|---|---|---|
Secret!99 | Length: 9 | Score: 100 |
abc123 | Length: 6 | Score: 40 |
Ab1! | Length: 4 | Score: 75 |
ABCDEFGH | Length: 8 | Score: 45 |
| empty string | Length: 0 | Score: 0 |
