Problem overview
The checker now has two rules. A password is Strong only if it has at least 8 characters and contains at least one digit; otherwise it is Weak. The program also prints the facts it used. For Secret99 the output is:
Length: 8
Has digit: True
Strong
For abcdefgh the length is 8 but there is no digit, so the output is Length: 8, Has digit: False and Weak. Note the capital letters in True and False: they are text the program chooses, not what TypeScript prints for a boolean.
Approach: a flag that flips once
The length is one call away, but "contains a digit" needs a look at every character. The standard pattern is a boolean flag. It starts as false, meaning "no digit seen yet", and a loop over the characters sets it to true the moment one digit appears. Nothing in the loop ever sets it back, so after the loop the flag answers the question for the whole string.
A flag is the better tool here than a counter or an early return because the program does not need to know where the digit is or how many there are. Once the flag is true, the answer can't change.
The check for a single character is a regular expression, /[0-9]/.test(c). The part between the slashes is a pattern, and [0-9] matches one character in the range 0 to 9. The method .test returns true or false.
Following the loop on Secret99: for the first six characters, S, e, c, r, e and t, the test is false and hasDigit stays false. At index 6 the character is 9, the test is true and the flag flips. The last character, another 9, sets it to true again, which changes nothing. The loop could stop at the first digit, but it doesn't need to: with passwords this short, finishing the pass is simpler and just as correct.
Step by step
- Store
lengthas aconstnumber, since it is used for the printed line and for the rule. - Print the first line,
"Length: "and the number. - Create
hasDigitwithlet, typedboolean, starting atfalse. It isletbecause it changes. - Loop over the characters with
for (const c of password). A string is iterable, andfor...ofgives one character per pass. - Test each character. Inside the loop, if
/[0-9]/.test(c)is true, sethasDigittotrue. - Print the flag as text after the loop. A ternary picks the word:
hasDigit ? "True" : "False". It sits in parentheses so that+joins the finished word to the label. - Write the verdict. The condition
length >= 8 && hasDigitis true only when both parts are true, and that case printsStrong. - Add the
else, which printsWeakfor everything else: too short, no digit, or both.
The solution in TypeScript
const password: string = "Secret99";
const length: number = password.length;
console.log("Length: " + length);
let hasDigit: boolean = false;
for (const c of password) {
if (/[0-9]/.test(c)) {
hasDigit = true;
}
}
console.log("Has digit: " + (hasDigit ? "True" : "False"));
if (length >= 8 && hasDigit) {
console.log("Strong");
} else {
console.log("Weak");
}
Details that matter:
- Printing a boolean directly gives lowercase.
"Has digit: " + hasDigitwould printHas digit: true. The required text isTrue, so the ternary supplies it. - The parentheses around the ternary matter. Without them,
+binds tighter than?:, so the label and the flag are joined first, and the ternary would test that whole non-empty string, which is always truthy. WithhasDigitfalse, the lineconsole.log("Has digit: " + hasDigit ? "True" : "False")prints justTrue. &&stops early. Iflength >= 8is false, the second operand isn't evaluated. Here that is harmless, becausehasDigitis already computed.[0-9]matches only the ASCII digits. The pattern returnsfalsefor the Arabic-Indic digit three, for example, so only 0 to 9 count.
Complexity
The loop visits each of the n characters once and does a constant amount of work for each, so the time is O(n). The program stores a few variables whatever the password's size, so the extra space is O(1). The regular expression is a literal, and each call tests a single character, so it adds no hidden cost per pass.
Edge cases and common mistakes
- Declaring
hasDigitwithconst. The assignment inside the loop would be an error. Uselet. - Setting the flag to
falsein anelseinside the loop. Then a digit followed by a letter would erase the result. The flag only ever moves fromfalsetotrue. - Printing the flag inside the loop. The line must appear once, after the loop has seen every character.
- Using
||instead of&&. A password with 8 letters and no digit would be reported asStrong. - Short passwords with a digit.
abc12has a digit but only 5 characters, so the output isLength: 5,Has digit: TrueandWeak. Likewise12345678passes both rules and isStrong: the checker only tests the two rules.
Testing
Change password in the editor above:
| password | Length | Has digit | Verdict |
|---|---|---|---|
Secret99 | 8 | True | Strong |
abcdefgh | 8 | False | Weak |
abc12 | 5 | True | Weak |
12345678 | 8 | True | Strong |
