Problem overview
Some passwords are weak whatever their characters: everybody tries qwerty first. This version rejects passwords that appear in a list of well-known ones. The list is an object, common, whose keys are the known weak passwords. The program prints the password, whether it is common, and a verdict.
For "qwerty" the output is:
Password: qwerty
Common: True
Weak: reject
For "Tr0ub4dor", which is not in the list, it is:
Password: Tr0ub4dor
Common: False
Strong: accept
Approach: one lookup instead of a chain of comparisons
You could compare the password with each entry in turn: password === "password" || password === "123456" || .... That grows with every word you add, and it means scanning the whole list. An object answers "is this a key?" directly. The value stored under each key doesn't matter here; the keys are the data, and true is just a placeholder.
The program asks the question once, saves the answer in a boolean, isCommon, and uses that boolean twice: to print the Common line and to choose the verdict.
Step by step
- Print the password line:
"Password: "joined withpassword. - Look it up.
password in commonistruewhen the text is one of the object's keys, andfalseotherwise. Store it in the constantisCommon. - Print the
Commonline. A boolean prints as lowercasetrue, but the output needsTrue, so choose the text with the ternaryisCommon ? "True" : "False", inside parentheses. - Write the verdict's
if. WhenisCommonis true, printWeak: reject. A common password is rejected whatever else is true of it. - Add the
else. It printsStrong: accept.
The solution in JavaScript
const common = {"password": true, "123456": true, "qwerty": true, "letmein": true};
const password = "qwerty";
console.log("Password: " + password);
const isCommon = password in common;
console.log("Common: " + (isCommon ? "True" : "False"));
if (isCommon) {
console.log("Weak: reject");
} else {
console.log("Strong: accept");
}
Details worth knowing
The condition if (isCommon) needs no comparison, because isCommon is already a boolean. Writing if (isCommon === true) would work but says the same thing in more words.
An object finds a key in roughly constant time, however many keys it holds, so a list of thousands of common passwords costs no more per lookup than a list of four.
The match is exact. "Qwerty" with a capital is not a key of common, so it prints Common: False. A realistic checker would lower-case the password first, with password.toLowerCase().
A trap: inherited keys
The in operator also looks at inherited properties. Every plain object inherits names such as toString and constructor, so "toString" in common is true, even though that word is not in the list. With password = "toString" the program prints Common: True and Weak: reject. That is harmless for this list, but it is a real difference between "is a key of this object" and "is one of my entries".
Two safer options exist. Object.hasOwn(common, password) checks only the object's own keys: it gives true for "qwerty" and false for "toString". A Set built with new Set(["password", "123456", "qwerty", "letmein"]) holds just those words, and .has("toString") is false. The statement's mention of a set refers to this second option.
Complexity
The program does one lookup and a few prints, so it runs in O(1) time on average, apart from the cost of building the object, which is O(m) for m known passwords.
Edge cases and common mistakes
- Printing the boolean directly.
"Common: " + isCommongivesCommon: true, which doesn't match the requiredTrue. - Leaving out the parentheses around the ternary. The
+would run first, and the output would lose its label. - Case differences.
"QWERTY"is not found unless you convert it to lower case first. - Checking the value instead of the key.
common[password]isundefinedfor a missing key, which is falsy, butinexpresses the intent better. - Printing both verdicts. The
elsemakes sure only one prints.
Testing
Change password in the editor above:
| password | Common line | Verdict |
|---|---|---|
"qwerty" | Common: True | Weak: reject |
"Tr0ub4dor" | Common: False | Strong: accept |
"Qwerty" | Common: False | Strong: accept |
