Count how many characters of each class the password contains.
You start with:
const password: string = "Hello!World9";
Build:
Uppercase: 2 Lowercase: 8 Digits: 1 Symbols: 1
Rules:
- Use four integer counters, all starting at 0.
- Loop through each character and add 1 to the matching counter.
- Treat any character that is not a letter and not a digit as a symbol.
- Print one line per counter, in this order:
"Uppercase: ","Lowercase: ","Digits: ","Symbols: ", each followed by the count.
Try other passwords: with Pass word!2 the output is
Uppercase: 1 Lowercase: 7 Digits: 1 Symbols: 2
Use
uppercase++, etc. to increment integer counters.
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);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
