Problem overview
This version checks four kinds of character: uppercase letters, lowercase letters, digits and symbols. A symbol is any character that is neither a letter nor a digit. The program prints the length, one line for each kind, and a verdict. The password is Strong only if it has at least 8 characters and all four kinds.
For "Secret!99" the output is:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
For "secret99", which has no capital and no symbol, it is:
Length: 8
Upper: False
Lower: True
Digit: True
Symbol: False
Weak
Approach: several flags, one pass
The previous version used one flag, hasDigit. This one uses four flags, hasUpper, hasLower, hasDigit and hasSymbol, each starting as false. A single loop visits every character once, and each character can switch on the flag that matches it. There is no need to loop four times.
Inside the loop, the four tests are four separate if statements, not an if/else if chain. A chain would stop at the first match, which is fine for exclusive categories but means each test would depend on the ones above it. Four independent ifs are easier to read: each says what it checks and what it sets.
The symbol test is defined as the complement of the letters and digits, not as a list of allowed punctuation. That means no character is left without a class: every character is a letter, a digit or a symbol. It also means a space, a dash or an accented letter counts as a symbol without any extra code.
Step by step
- Store and print the length, as in the earlier versions.
- Create
hasUpperandhasLowerwithlet, bothfalse. - Create
hasDigitandhasSymbolthe same way. - Start a
for...ofloop overpassword, withconst cas the current character. - Uppercase test:
/[A-Z]/.test(c)setshasUpper. - Lowercase test:
/[a-z]/.test(c)setshasLower, in its ownif. - Digit test:
/[0-9]/.test(c)setshasDigit. - Symbol test:
!/[A-Za-z0-9]/.test(c)setshasSymbol. The pattern matches letters and digits, and the!in front turnstrueintofalseand back, so the test is true for anything else. - Print the four flag lines after the loop, in the order Upper, Lower, Digit, Symbol, each as
TrueorFalsewith the ternary. - Write the verdict. Five conditions joined with
&&: the length and the four flags. PrintStrongif all hold. - Add the
elsethat printsWeak.
The solution in JavaScript
const password = "Secret!99";
const length = password.length;
console.log("Length: " + 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;
}
}
console.log("Upper: " + (hasUpper ? "True" : "False"));
console.log("Lower: " + (hasLower ? "True" : "False"));
console.log("Digit: " + (hasDigit ? "True" : "False"));
console.log("Symbol: " + (hasSymbol ? "True" : "False"));
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
console.log("Strong");
} else {
console.log("Weak");
}
Following the flags through the loop
This table shows the four flags, in the order upper, lower, digit, symbol, with 1 for true and 0 for false, after each character of "Ab1!":
| Character | Flags afterwards |
|---|---|
A | 1000 |
b | 1100 |
1 | 1110 |
! | 1111 |
Each character switches on exactly one flag and leaves the other three alone. Flags that are already on stay on, because nothing in the loop ever sets a flag to false. After the last character all four are on, so the four conditions in the verdict pass. The fifth condition, the length, fails for this short password, which is why "Ab1!" is still Weak.
Details worth knowing
The ranges A-Z, a-z and 0-9 are ASCII ranges. A letter such as é is outside them, so /[A-Za-z0-9]/.test("é") is false, and the symbol test counts é as a symbol. That follows the lesson's definition, which is "not a letter and not a digit" in the ASCII sense. A broader check would need Unicode-aware patterns.
The ! operator negates a boolean, and it applies to the whole call /[A-Za-z0-9]/.test(c). It reads as "it is not true that c is a letter or digit".
A flag line such as "Upper: " + (hasUpper ? "True" : "False") needs its parentheses. Without them, + runs first, and the condition would be the non-empty text "Upper: true" instead of the flag.
The verdict repeats && five times. It short-circuits: as soon as one operand is false, the rest are not evaluated. Here that makes no difference to the result, only to how much work is done.
Edge cases and common mistakes
- Using
else ifbetween the four tests. It would still work for these four categories, because a character belongs to only one, but it hides the independence of the tests and breaks if you later add a class that overlaps another. - Forgetting the length rule.
"Ab1!"has all four kinds, but only 4 characters, so it isWeak. - Missing one kind.
"Secret99"has 8 characters and three kinds. It has no symbol, so it isWeak. - Treating a space as a letter. Spaces count as symbols here.
- Putting the flag lines inside the loop. They would print once per character.
Testing
Try these in the editor above. In each case only the Strong password has all five conditions.
| password | Upper | Lower | Digit | Symbol | Verdict |
|---|---|---|---|---|---|
"Secret!99" | True | True | True | True | Strong |
"secret99" | False | True | True | False | Weak |
"Secret99" | True | True | True | False | Weak |
"SECRET!99" | True | False | True | True | Weak |
"Ab1!" | True | True | True | True | Weak |
