Problem overview
Some passwords are weak no matter how they are built, because everybody uses them. This version keeps a list of well-known passwords and rejects any password found on it. The starter provides the list as an object, common, of type Record<string, boolean>, whose keys are password, 123456, qwerty and letmein, each with the value true. It also provides the password to check, qwerty.
The program prints the password, whether it is common, and a verdict:
Password: qwerty
Common: True
Weak: reject
For Tr0ub4dor the output is Password: Tr0ub4dor, Common: False and Strong: accept. Here "Strong" only means "not on the list": the earlier length and character checks aren't part of this version.
Approach: a lookup table
A first idea is to compare the password with each common one in turn, using a chain of === tests or a loop over an array. That works for four entries, but it does more work as the list grows: a list of a million passwords would be scanned one by one. An object, like a Map or a Set, is built for lookups. Finding out whether a key is present takes about the same time however many keys there are.
The question "is this password a key of common?" is answered by the in operator: password in common is true or false. The program stores that boolean in isCommon, then uses it twice: once to print it, once to choose the verdict.
Step by step
- Print the first line:
"Password: "followed by the password. Both are strings, so+joins them. - Compute
isCommonas aconstboolean:password in common. Note thatinasks about keys, not values. - Print the second line. A boolean prints as lowercase
trueorfalse, but the output needsTrueorFalse. The ternaryisCommon ? "True" : "False"chooses the text, and the parentheses make sure it is chosen before it is joined to the label. - Write the
if: whenisCommonis true, printWeak: reject. The condition is the boolean itself, with no comparison. - Add the
else: printStrong: accept. For the sample password this branch is skipped.
The solution in TypeScript
const common: Record<string, boolean> = {"password": true, "123456": true, "qwerty": true, "letmein": true};
const password: string = "qwerty";
console.log("Password: " + password);
const isCommon: boolean = password in common;
console.log("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
console.log("Weak: reject");
} else {
console.log("Strong: accept");
}
Details about this code:
- The values in
commonare never read. Only the keys matter, andtrueis just a placeholder so each key has a value. ASetexpresses the same idea without placeholders:new Set<string>(["password", "123456", "qwerty", "letmein"])withcommon.has(password). intests presence, not truthiness. For an object{"hunter2": false},"hunter2" in oddistrue, whileodd["hunter2"]isfalse. The lesson's list holds onlytruevalues, so both would agree, butinis the more direct question.- Keys are strings. The key
"123456"is written with quotes in the object, and the password is a string too, so the comparison is string against string. - Types are erased.
Record<string, boolean>and thebooleanannotation vanish when the code is compiled to JavaScript.
Complexity
One lookup in an object takes constant time on average, so the check is O(1) in the number of common passwords, plus the time to hash the password's characters, proportional to its length. The object itself takes O(m) space for m common passwords.
Edge cases and common mistakes
- Case matters. The keys are lower case, so
Qwertyis not found. The output isCommon: FalseandStrong: accept. Comparingpassword.toLowerCase()would catch that variant, but this lesson does not ask for it. - Inherited names. An object literal inherits properties such as
toString. For the passwordtoString,inreturnstrue, so the program printsCommon: TrueandWeak: rejecteven though that word is not in the list.Object.hasOwn(common, password)checks only the object's own keys and givesfalse. ASetavoids this problem too. - Printing the boolean directly.
"Common: " + isCommonprintsCommon: true, in lower case, which doesn't match the expected output. - Using the wrong operator. In JavaScript,
inchecks keys.password in commonnever looks at the values. Using it on an array would test the array's indexes, not its items. - Both branches printing. The verdict is one
if/else, so exactly one of the two lines is printed.
Testing
Change password in the editor above. These results come from the reference solution:
| password | Common | Verdict line |
|---|---|---|
qwerty | True | Weak: reject |
Tr0ub4dor | False | Strong: accept |
Qwerty | False | Strong: accept |
toString | True | Weak: reject |
