Problem overview
This is the first version of a small password strength checker, and it looks at one thing only: how many characters the password has. The starter gives a constant, const password: string = "Secret99";. The program prints the length on one line, then a verdict on the next: OK when the length is at least 8, Too short otherwise.
Password length: 8
OK
With the password abc the output is Password length: 3 followed by Too short.
Approach: measure once, then decide
The length is needed twice, once to print it and once to compare it with 8. Storing it in a variable avoids asking the string twice and gives the number a name. After that, a single if/else chooses which of the two words to print.
The comparison is length >= 8, because the rule says "at least 8", so a password of exactly 8 characters passes. The else branch needs no condition of its own: it runs for every length the if rejected, which is 7 or fewer.
A length rule is the weakest possible check, and that is the point of starting here. A password like aaaaaaaa passes it, and so does 12345678. The next version builds on this one.
Step by step
- Store the length. Create a
constnamedlength, typed asnumber, frompassword.length. In TypeScriptlengthis a property of a string, so it has no parentheses. - Print the first line. Join the label
"Password length: "and the number with+, which converts the number to text because the other operand is a string. - Write the
if. Whenlength >= 8, printOK. - Add the
else. It printsToo short. Writeelseon the same line as the closing brace of theif.
The solution in TypeScript
const password: string = "Secret99";
const length: number = password.length;
console.log("Password length: " + length);
if (length >= 8) {
console.log("OK");
} else {
console.log("Too short");
}
Some details for beginners:
constis right forlengthbecause it is assigned once and never changes.- The types,
stringandnumber, are checked by the compiler and then erased. The JavaScript that runs has no types. "Password length: " + lengthjoins text and a number into text. If both operands were numbers,+would add them instead, so the text always has to be on one side.
Printing the first line before the verdict matters too: the output is two lines in a fixed order, so the first console.log must come before the if, and the verdict is printed by exactly one of the two branches.
Edge cases and common mistakes
- Writing
> 8instead of>= 8. The sample password has exactly 8 characters and would be rejected. - Writing
password.length().lengthis a number, not a function. The compiler reportsThis expression is not callable, and the JavaScript that still gets written fails at run time withTypeError: password.length is not a function. - The empty string. With
""the output isPassword length: 0andToo short. - Spaces count.
"Pass word"has 9 characters, and the output isPassword length: 9andOK. - Length counts UTF-16 code units, not what you see as characters. A single emoji such as the grinning face has
length2, while[...password].lengthcounts it as 1. Plain letters and digits, like the ones in this lesson, always count as 1 each.
Testing
Change password in the editor above and compare:
| password | output |
|---|---|
Secret99 | Password length: 8, then OK |
abc | Password length: 3, then Too short |
| empty string | Password length: 0, then Too short |
Pass word | Password length: 9, then OK |
