Track four boolean flags for a password: does it contain an uppercase letter, a lowercase letter, a digit, and a symbol (non-alphanumeric)?
You start with:
const password: string = "Secret!99";
Build:
Length: 9 Upper: True Lower: True Digit: True Symbol: True Strong
Rules:
- Print
"Length: "followed by the number of characters. - Start four flags (
hasUpper,hasLower,hasDigit,hasSymbol) atfalse. Loop over every character and set the matching flag totrue. - A symbol is any character that is not a letter and not a digit.
- Print one line per flag, in this order:
"Upper: ","Lower: ","Digit: ","Symbol: ", each followed by the flag. Write it asTrueorFalsewith a capital letter, exactly as in the example (your language's own boolean prints differently, so choose the text yourself). - If the length is at least 8 and all four flags are
true(stricter than the last lesson), printStrong; otherwise printWeak.
Try other passwords: with secret99 the output is
Length: 8 Upper: False Lower: True Digit: True Symbol: False Weak
Use regex tests like
/[A-Z]/.test(c) for each class. Symbol = !/[A-Za-z0-9]/.test(c).
const password: string = "Secret!99";
const length: number = password.length;
console.log("Length: " + length);
let hasUpper: boolean = false;
let hasLower: boolean = false;
let hasDigit: boolean = false;
let hasSymbol: boolean = false;
for (const c of password) {
if (/[A-Z]/.test(c)) {
hasUpper = true;
}
if (/[a-z]/.test(c)) {
hasLower = true;
}
if (/[0-9]/.test(c)) {
hasDigit = true;
}
if (!/[A-Za-z0-9]/.test(c)) {
hasSymbol = true;
}
}
console.log("Upper: " + (hasUpper ? "True" : "False"));
console.log("Lower: " + (hasLower ? "True" : "False"));
console.log("Digit: " + (hasDigit ? "True" : "False"));
console.log("Symbol: " + (hasSymbol ? "True" : "False"));
if (length >= 8 && hasUpper && hasLower && hasDigit && hasSymbol) {
console.log("Strong");
} else {
console.log("Weak");
}
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
