Problem overview
This is the first version of a small password checker. It starts with a password in a constant, password, and prints two lines: the number of characters, and a verdict. A password of at least 8 characters is OK, and a shorter one is Too short. The only thing the program looks at is the length.
For "Secret99" the output is:
Password length: 8
OK
For "abc" it is:
Password length: 3
Too short
Approach: measure once, use the number twice
The length is needed in two places: it goes into the first line of output, and the verdict compares it with 8. So the program reads it once, stores it in a constant named length, and uses that name in both places. Then a two-way if/else chooses which verdict to print.
The boundary is the part to get right. The rule says "at least 8", so a password with exactly 8 characters passes. In code, "at least" is >=.
Step by step
- Store the length. A string's
.lengthproperty holds its number of characters. It is a property, so there are no parentheses:password.length, notpassword.length(). - Print the first line. Join the label
"Password length: "and the number with+. When one side of+is a string, JavaScript turns the number into text. - Write the
if. Its condition islength >= 8, and its body printsOK. - Add the
else. It runs for every length theifdid not take, which is 7 or fewer, and printsToo short. Theelsegoes on the same line as the closing brace of theif.
The solution in JavaScript
const password = "Secret99";
const length = password.length;
console.log("Password length: " + length);
if (length >= 8) {
console.log("OK");
} else {
console.log("Too short");
}
Details worth knowing
Calling password.length() with parentheses is a common slip. length holds a number, and a number can't be called like a function, so JavaScript throws a TypeError. In JavaScript, length is a plain property of strings and arrays alike.
The constant is named length, which is also the name of a property. That is fine: length on its own is the constant, and password.length is the property of the string.
The .length of a string counts UTF-16 code units, not what a person would call characters. For ordinary letters and digits the two are the same. A single emoji such as "😀" has a length of 2, so a password made of four emoji would pass the length rule of 8. The lesson's rule is about the number .length gives, so the program follows it.
The if/else runs exactly one of its two branches. There is no input for which both lines print, or neither, and that guarantees one verdict.
Edge cases and common mistakes
- Using
>instead of>=. A password of exactly 8 characters would be rejected, so"abcdefgh"would printToo shortalthough it is 8 characters long. - Off by one the other way.
length >= 7would accept 7 characters. - The empty string.
""has a length of 0, so the output isPassword length: 0andToo short. - Printing the number and the text on separate lines. The first line is the label and the number together, so it needs one
console.log. - Typing a space after
OK. Output is compared exactly, so keep the text as given.
Testing
Replace the password in the editor above and compare with these results. Lengths 7 and 8 are the interesting pair.
| password | Output |
|---|---|
"Secret99" | Password length: 8, then OK |
"abcdefgh" | Password length: 8, then OK |
"abcdefg" | Password length: 7, then Too short |
"" | Password length: 0, then Too short |
Later versions of this project add more rules to the same program. Keeping the length in a named constant, as here, is the habit that makes such additions easy.
