Problem overview
The checker now looks at 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 per kind with True or False, and then a verdict. The password is now Secret!99:
Length: 9
Upper: True
Lower: True
Digit: True
Symbol: True
Strong
The verdict is stricter than in the previous version. It needs at least 8 characters and all four flags true; any miss gives Weak. For secret99 the output is Length: 8, Upper: False, Lower: True, Digit: True, Symbol: False and Weak.
Approach: one loop, four independent flags
The previous version used one flag for one question. Here the same pattern is repeated four times: each flag starts at false, and a loop over the characters flips it when it sees a matching character. One pass over the string is enough to update all four flags.
Inside the loop the four tests are four separate if statements, not an if / else if chain. The four classes never overlap for a single character, so a chain would give the same result here. Separate ifs are still the safer shape: each rule stands alone, and adding or changing one rule can't break another.
On Secret!99, the S flips hasUpper. The letters e, c, r, e, t flip hasLower. The ! flips hasSymbol and the two 9s flip hasDigit. After nine passes all four flags are true, and the length is 9, so the verdict is Strong.
The symbol test is defined by exclusion. The pattern /[A-Za-z0-9]/ matches a letter or a digit, so putting ! in front of the test gives "not a letter and not a digit". That avoids listing every possible symbol character.
Step by step
- Store
lengthand print the first line. - Create
hasUpperandhasLowerwithlet, typedboolean, bothfalse. - Create
hasDigitandhasSymbolthe same way. - Open a
for (const c of password)loop and close its brace at once. - Uppercase test. If
/[A-Z]/.test(c), sethasUppertotrue. - Lowercase test. If
/[a-z]/.test(c), sethasLowertotrue. - Digit test. If
/[0-9]/.test(c), sethasDigittotrue. - Symbol test. If
!/[A-Za-z0-9]/.test(c), sethasSymboltotrue. - After the loop, print the four flag lines in the order Upper, Lower, Digit, Symbol, each with a ternary that turns the boolean into
TrueorFalse. - Write the verdict. One
ifjoins five conditions with&&: the length and the four flags. TheelseprintsWeak.
The solution in TypeScript
const password: string = "Secret!99";
const length: number = password.length;
console.log("Length: " + 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;
}
}
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");
}
Language details to know:
for...ofon a string walks it one character at a time. Characters outside the basic range, such as emoji, come out as one item each; that matters little here, because they count as symbols either way.!/[A-Za-z0-9]/.test(c)reads as "not (the test)". The!applies to the result of.test(c), which is a boolean.- The ranges are ASCII only. A letter such as
ématches none of[A-Z],[a-z]or[0-9], so it counts as a symbol. With the passwordSecret99éthe output isSymbol: Trueand the verdict isStrong, even though there is no punctuation. - The parentheses around each ternary are needed so the word is chosen before it is joined to the label.
It is worth seeing why the flags are computed before anything is printed. The first flag line must show a fact about the whole password, and the loop has not finished until the last character has been tested. Printing inside the loop would produce nine copies of each line for a nine-character password. Computing first, printing second, and deciding last gives three clean phases: collect the facts, report them, judge them.
The verdict also shows how && chains: length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol is true only when every part is true, and evaluation stops at the first false part. The flags are plain booleans, so they can be used as conditions directly; writing hasUpper === true adds nothing.
Complexity
There is one pass over the n characters, with four constant-time tests per character, so the time is O(n). The space is O(1): four booleans and a few other variables.
Edge cases and common mistakes
- A space is a symbol. It is not a letter or a digit, so
Secret 99printsSymbol: True. - Forgetting the length rule.
Abc1!has all four classes, but it has only 5 characters, so the output ends withWeak. - Omitting the
!in the symbol test. ThenhasSymbolwould mean "has a letter or digit" and nearly every password would showSymbol: True. - Printing the flags in the wrong order. The lines must follow Upper, Lower, Digit, Symbol.
Testing
Change password in the editor above. These results come from the reference solution:
| password | Upper | Lower | Digit | Symbol | Verdict |
|---|---|---|---|---|---|
Secret!99 | True | True | True | True | Strong |
secret99 | False | True | True | False | Weak |
Secret 99 | True | True | True | True | Strong |
Abc1! | True | True | True | True | Weak |
