Start learning free

LESSON 199 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Password Strength V5 in TypeScript: Rejecting Common Passwords

Practice Read the full explanation

Use a dictionary (or set) as a fast lookup table to reject well-known weak passwords.

You start with:

const common: Record<string, boolean> = {"password": true, "123456": true, "qwerty": true, "letmein": true};
const password: string = "qwerty";

Build:

Password: qwerty
Common: True
Weak: reject

Rules:

  • Print "Password: " followed by the password.
  • Look the password up in the object common and store the result as a boolean.
  • Print "Common: " followed by the boolean. Write it as True or False with a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself).
  • If it is common, print "Weak: reject"; otherwise print "Strong: accept".

Try other passwords: with Tr0ub4dor the output is

Password: Tr0ub4dor
Common: False
Strong: accept

password in common checks whether password is a key of the object common.

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");
}

STEP 1 OF 5
    Ctrl + Enter to check

    TYPESCRIPT · PROJECTS: EVERYDAY PROGRAMS

    Password Strength V5 in TypeScript, explained

    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

    1. Print the first line: "Password: " followed by the password. Both are strings, so + joins them.
    2. Compute isCommon as a const boolean: password in common. Note that in asks about keys, not values.
    3. Print the second line. A boolean prints as lowercase true or false, but the output needs True or False. The ternary isCommon ? "True" : "False" chooses the text, and the parentheses make sure it is chosen before it is joined to the label.
    4. Write the if: when isCommon is true, print Weak: reject. The condition is the boolean itself, with no comparison.
    5. Add the else: print Strong: 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 common are never read. Only the keys matter, and true is just a placeholder so each key has a value. A Set expresses the same idea without placeholders: new Set<string>(["password", "123456", "qwerty", "letmein"]) with common.has(password).
    • in tests presence, not truthiness. For an object {"hunter2": false}, "hunter2" in odd is true, while odd["hunter2"] is false. The lesson's list holds only true values, so both would agree, but in is 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 the boolean annotation 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 Qwerty is not found. The output is Common: False and Strong: accept. Comparing password.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 password toString, in returns true, so the program prints Common: True and Weak: reject even though that word is not in the list. Object.hasOwn(common, password) checks only the object's own keys and gives false. A Set avoids this problem too.
    • Printing the boolean directly. "Common: " + isCommon prints Common: true, in lower case, which doesn't match the expected output.
    • Using the wrong operator. In JavaScript, in checks keys. password in common never 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:

    passwordCommonVerdict line
    qwertyTrueWeak: reject
    Tr0ub4dorFalseStrong: accept
    QwertyFalseStrong: accept
    toStringTrueWeak: reject
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free