Problem overview
The previous version asked whether a password has each kind of character. This version asks how many. The program counts the uppercase letters, the lowercase letters, the digits and the symbols, where a symbol is any character that is not a letter and not a digit, and prints four lines.
For "Hello!World9" the output is:
Uppercase: 2
Lowercase: 8
Digits: 1
Symbols: 1
For "Pass word!2", where the space and the exclamation mark are both symbols, it is:
Uppercase: 1
Lowercase: 7
Digits: 1
Symbols: 2
Approach: four counters instead of four flags
A boolean flag can only say "seen" or "not seen". A number can say how many. So the four flags become four integer counters, each starting at 0. The loop visits each character once, and when a character belongs to a class, it adds 1 to that class's counter with ++.
Each class has its own if inside the loop, with the same four patterns as before. The program has no verdict this time; it only reports the counts, after the loop has finished.
Counting gives the program more information than flags did. A flag can't tell a password with one digit from a password with ten, but a counter can. This version only reports the counts; it makes no decision about them.
Step by step
- Create four counters with
let, one at a time:uppercase,lowercase,digitsandsymbols, each set to 0. They useletbecause they change while the loop runs. - Start a
for...ofloop overpassword. It runs once per character: 12 times for"Hello!World9". - Uppercase:
if (/[A-Z]/.test(c))adds 1 touppercase. - Lowercase:
/[a-z]/adds 1 tolowercase. - Digit:
/[0-9]/adds 1 todigits. - Symbol:
!/[A-Za-z0-9]/.test(c)adds 1 tosymbols. Letters and digits make this false, and anything else, such as!or a space, makes it true. - Print the four lines after the loop, in the order Uppercase, Lowercase, Digits, Symbols. Each is a label joined to a number with
+.
The solution in JavaScript
const password = "Hello!World9";
let uppercase = 0;
let lowercase = 0;
let digits = 0;
let symbols = 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);
Following the counters through the loop
These are the four counters, in the order uppercase, lowercase, digits, symbols, after each character of "Hello!World9":
| Character | Counters afterwards |
|---|---|
H | 1, 0, 0, 0 |
e | 1, 1, 0, 0 |
l | 1, 2, 0, 0 |
l | 1, 3, 0, 0 |
o | 1, 4, 0, 0 |
! | 1, 4, 0, 1 |
W | 2, 4, 0, 1 |
o | 2, 5, 0, 1 |
r | 2, 6, 0, 1 |
l | 2, 7, 0, 1 |
d | 2, 8, 0, 1 |
9 | 2, 8, 1, 1 |
Every character raises exactly one counter by one. The last row, 2, 8, 1, 1, is what the four console.log calls print. If you ever see counters that don't add up to the length, a test is wrong or a character class is missing.
Details worth knowing
uppercase++ is shorthand for uppercase = uppercase + 1. Used as a statement on its own line, it simply adds one.
The four tests are independent ifs. For any character, at most one of them is true, so the four counters add up to the number of characters the loop visited. For "Hello!World9" that is 2 + 8 + 1 + 1 = 12.
A for...of loop over a string goes through code points, while .length counts UTF-16 code units. For "😀a", .length is 3, but the loop sees two characters. The emoji is neither a letter nor a digit, so the program prints Symbols: 1 and Lowercase: 1. Likewise "héllo" prints Lowercase: 4 and Symbols: 1, because é is outside the ASCII range a-z and so counts as a symbol under the lesson's rule.
The numbers join the label as text through +. The counter itself stays a number, which is why uppercase++ keeps working after the loop. If a counter were a string, ++ would convert it to a number first.
Flags and counters side by side
Compared with the last version, three things change. The starting values are 0 instead of false. The update is uppercase++ instead of hasUpper = true. And the output prints a number, so no ternary is needed to pick True or False. The loop and the four patterns are identical, which is what makes it easy to move from one version to the other.
Complexity
The loop runs once per character and does four constant-time tests each time, so the time is O(n) for a password of length n. The program stores four numbers, so the extra space is O(1).
Edge cases and common mistakes
- Declaring a counter with
const.uppercase++would throw aTypeError. - Resetting a counter inside the loop. The count would restart on every character.
- Printing inside the loop. You would get four lines for each character.
- Forgetting that the space is a symbol. In
"Pass word!2", both the space and!count. - Starting a counter at 1. Every count would be one too high, and a password with no digits would report
Digits: 1. - The empty string. The loop doesn't run, and all four counts are 0.
- The wrong order of the output lines. Keep Uppercase, Lowercase, Digits, Symbols.
Testing
Try these in the editor above:
| password | Uppercase | Lowercase | Digits | Symbols |
|---|---|---|---|---|
"Hello!World9" | 2 | 8 | 1 | 1 |
"Pass word!2" | 1 | 7 | 1 | 2 |
"" | 0 | 0 | 0 | 0 |
