Problem overview
This version stops asking "is there at least one?" and asks "how many?". For the password Hello!World9 the program prints four counters, one per kind of character:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
The uppercase letters are H and W, the digit is 9, the symbol is !, and the other eight characters are lowercase. Any character that is not a letter and not a digit counts as a symbol, and that includes a space: for Pass word!2 the output is Uppercase: 1, Lowercase: 7, Digits: 1 and Symbols: 2, with the space and the ! as the two symbols. The previous version used booleans to remember that a class had been seen; this one uses integers to remember how often.
Approach: four counters and one pass
A counter is a variable that starts at 0 and grows by one each time something is found. The program needs four of them, one for each class. A single loop over the characters tests each character against the four classes, and each matching test adds one to its counter.
The classes are defined with regular expressions. /[A-Z]/ matches one capital letter, /[a-z]/ one small letter, and /[0-9]/ one digit. The symbol test is the complement: !/[A-Za-z0-9]/.test(c) is true when the character is neither a letter nor a digit, so no list of symbols is needed.
Every character lands in exactly one class, which gives a handy self-check: the four counters always add up to the length of the password. For Hello!World9, 2 + 8 + 1 + 1 is 12, and the string has 12 characters.
Why four separate variables and not a table? With a fixed, small set of classes, named counters are the clearest choice: the output lines read straight from them. A table, such as an object keyed by class name, pays off when the set of keys isn't known beforehand. Here it is known, so plain variables keep the code short and every name tells you what it holds.
Step by step
- Create
uppercasewithlet, typednumber, set to 0. - Create
lowercasethe same way. - Create
digitsthe same way. - Create
symbolsthe same way. The counters useletbecause their values change while the loop runs. - Open
for (const c of password)and close its brace right away. The loop runs once per character, 12 times for the sample password. - Uppercase test. If
/[A-Z]/.test(c), douppercase++. - Lowercase test. If
/[a-z]/.test(c), dolowercase++. - Digit test. If
/[0-9]/.test(c), dodigits++. - Symbol test. If
!/[A-Za-z0-9]/.test(c), dosymbols++. - After the loop, print four lines, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined to a counter with
+, which turns the number into text.
The solution in TypeScript
const password: string = "Hello!World9";
let uppercase: number = 0;
let lowercase: number = 0;
let digits: number = 0;
let symbols: number = 0;
for (const c of password) {
if (/[A-Z]/.test(c)) {
uppercase++;
}
if (/[a-z]/.test(c)) {
lowercase++;
}
if (/[0-9]/.test(c)) {
digits++;
}
if (!/[A-Za-z0-9]/.test(c)) {
symbols++;
}
}
console.log("Uppercase: " + uppercase);
console.log("Lowercase: " + lowercase);
console.log("Digits: " + digits);
console.log("Symbols: " + symbols);
Details worth knowing:
x++adds 1 tox; it is short forx = x + 1. It changes the variable, which is why the counters are declared withletand notconst.- All counts are
number. TypeScript has one number type for integers and decimals alike. Counts this small are exact, and"Digits: " + digitsprints a plain1. - The regular expressions cover ASCII letters only.
Üandïare not inA-Zora-z, so they are counted as symbols. For the passwordÜnï9the output isUppercase: 0,Lowercase: 1,Digits: 1andSymbols: 2. - The type annotations are erased when the file is compiled to JavaScript and don't change how the program runs.
The order of the four tests inside the loop doesn't affect the result, because each test only touches its own counter. The order of the four console.log lines does matter: the output must list Uppercase, Lowercase, Digits and then Symbols.
Complexity
The loop makes one pass over the n characters with four constant-time tests per character, so the time is O(n). The space is O(1): four counters, however long the password.
Edge cases and common mistakes
- An empty password. The loop never runs and all four lines show 0.
- Leaving out one class's test. Each class needs a test that runs for every character. Without it, that counter stays at 0 and the four counters no longer add up to the length.
- Declaring the counters with
const.uppercase++would then be an error, because a constant can't be changed. - Starting a counter without a value. A
letcounter that is never set holdsundefined, andundefined++givesNaN, so every counter must start at 0. - Forgetting the space. A space is a symbol, so passwords with spaces have a higher symbol count than the visible punctuation suggests.
- Printing inside the loop. The four lines must appear once, after the loop.
Testing
Change password in the editor above. These results come from the reference solution:
| password | Uppercase | Lowercase | Digits | Symbols |
|---|---|---|---|---|
Hello!World9 | 2 | 8 | 1 | 1 |
Pass word!2 | 1 | 7 | 1 | 2 |
| empty string | 0 | 0 | 0 | 0 |
Ünï9 | 0 | 1 | 1 | 2 |
