Password Strength Checker
What you'll build
A `checkPassword(password)` function that returns 'weak', 'medium', or 'strong' based on length, presence of uppercase, lowercase, numbers, and special characters.
Goals
- Combine multiple conditions using boolean logic
- Use string methods to inspect character types
- Build a scoring system that aggregates checks
- Return meaningful feedback based on a score
Prerequisites
- String methods (length, character access)
- if/else conditional statements
- Boolean logic with && and ||
Steps
- 1
Check the length
Start by checking if the password is at least 8 characters long. Length is the most basic strength indicator. Create a variable
scorestarting at 0 and add points for length.Codefunction checkPassword(password) { let score = 0; if (password.length >= 8) score += 1; if (password.length >= 12) score += 1; return score; }Expected result
checkPassword('short') returns 0; checkPassword('longpassword') returns 1.
- 2
Check for character variety
Use regular expressions to check for uppercase letters, lowercase letters, numbers, and special characters. Each type found adds a point to the score.
Codeif (/[a-z]/.test(password)) score += 1; if (/[A-Z]/.test(password)) score += 1; if (/[0-9]/.test(password)) score += 1; if (/[^a-zA-Z0-9]/.test(password)) score += 1;Expected result
A password with all four types gets 4 extra points.
- 3
Map score to strength label
Convert the numeric score to a human-readable label: score <= 2 is 'weak', 3-4 is 'medium', 5+ is 'strong'. Return the label instead of the raw score.
Codeif (score <= 2) return 'weak'; if (score <= 4) return 'medium'; return 'strong';Expected result
checkPassword('abc') returns 'weak'; checkPassword('Abc123!@') returns 'strong'.
- 4
Test with various passwords
Call the function with several test passwords to verify the scoring works correctly across different combinations.
Codeconsole.log(checkPassword('123')); // weak console.log(checkPassword('abcdefgh')); // weak console.log(checkPassword('Abcd1234')); // medium console.log(checkPassword('Str0ng!Pass#')); // strongExpected result
Four lines: weak, weak, medium, strong.
- 5
Add input validation
At the entry of the main logic, add input validation: check that the input is a valid number and within the allowed range, and show a clear message for invalid input instead of crashing. This is the first line of defense for a robust program.
Codefunction validateInput(value, min, max) { if (typeof value !== "number" || Number.isNaN(value)) { throw new Error(`Invalid input: ${value} is not a number`); } if (value < min || value > max) { throw new Error(`Input ${value} out of range [${min}, ${max}]`); } return value; } // Usage const input = validateInput(rawInput, 1, 100);Expected result
Invalid input (non-number or out of range) throws a clear error message; valid input passes through normally.
Hint
Wrap the call site in try/catch to show a friendly error to the user.
Try it yourself
function checkPassword(password) {
let score = 0;
// TODO: check length, character variety, and return a label
return score;
}function checkPassword(password) {
let score = 0;
if (password.length >= 8) score += 1;
if (password.length >= 12) score += 1;
if (/[a-z]/.test(password)) score += 1;
if (/[A-Z]/.test(password)) score += 1;
if (/[0-9]/.test(password)) score += 1;
if (/[^a-zA-Z0-9]/.test(password)) score += 1;
if (score <= 2) return 'weak';
if (score <= 4) return 'medium';
return 'strong';
}
console.log(checkPassword('123')); // weak
console.log(checkPassword('abcdefgh')); // weak
console.log(checkPassword('Abcd1234')); // medium
console.log(checkPassword('Str0ng!Pass#')); // strongWhat you learned
You combined multiple boolean conditions into a scoring system and used regular expressions to inspect string content — a pattern used in real-world validation logic.
Related Resources
Related Lessons
- Conditions
if/else branches drive the scoring logic.
- Strings and Text
String methods and regex inspect the password.
Related Practice
- Equality Operator Quiz
Practice comparison operators used in scoring.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.