Skip to content

Password Strength Checker

Easy ~25 min

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. 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 score starting at 0 and add points for length.

    Code
    function 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. 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.

    Code
      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;

    Expected result

    A password with all four types gets 4 extra points.

  3. 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.

    Code
      if (score <= 2) return 'weak';
      if (score <= 4) return 'medium';
      return 'strong';

    Expected result

    checkPassword('abc') returns 'weak'; checkPassword('Abc123!@') returns 'strong'.

  4. 4

    Test with various passwords

    Call the function with several test passwords to verify the scoring works correctly across different combinations.

    Code
    console.log(checkPassword('123'));           // weak
    console.log(checkPassword('abcdefgh'));     // weak
    console.log(checkPassword('Abcd1234'));     // medium
    console.log(checkPassword('Str0ng!Pass#')); // strong

    Expected result

    Four lines: weak, weak, medium, strong.

  5. 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.

    Code
    function 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.

Try it yourself

What 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

Related Practice

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic