Combine the earlier checks into a single numeric score between 0 and 100.
You start with:
const password = "Secret!99";
Build:
Length: 9 Score: 100
Rules:
- Start with
score = 0. - If the length is at least 8, add 25.
- If the password contains an uppercase letter, add 20.
- If it contains a lowercase letter, add 20.
- If it contains a digit, add 20.
- If it contains a symbol (not a letter and not a digit), add 15.
- Print
"Length: "followed by the length, then print"Score: "followed by the score.
Try other passwords: with abc123 the output is
Length: 6 Score: 40
Run the four character-class detections, then five independent
ifs that add to score.
const password = "Secret!99";
const length = password.length;
let hasUpper = false;
let hasLower = false;
let hasDigit = false;
let hasSymbol = 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;
}
}
let score = 0;
if (length >= 8) {
score += 25;
}
if (hasUpper) {
score += 20;
}
if (hasLower) {
score += 20;
}
if (hasDigit) {
score += 20;
}
if (hasSymbol) {
score += 15;
}
console.log("Length: " + length);
console.log("Score: " + score);
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
