Skip to the content.
3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 3.10 JS for Loops and Sprites

Big Idea 3.4 Hacks

In this lesson, we were taught string operations, such as string slicing [startindex:endindex], finding substrings .find(), upper or lower case .upper(), .lower(), measuing string length, len(), replacing substrings .replace(), splitting string, .split(), and joining strings .join().

// JavaScript hack for Password Validator

function pswd(password) {
    if (password.length < 8) {
        return "Password too short. Must be at least 8 characters.";
    }

    if (password === password.toLowerCase() || password === password.toUpperCase()) {
        return "Password must contain both uppercase and lowercase letters.";
    }

    if (!/[0-9]/.test(password)) {
        return "Password must contain at least one number.";
    }

    if (!/[!@#$%^&*()_+]/.test(password)) {
        return "Password must contain at least one special character (e.g. !, @, #, etc.)";
    }

    const commonPasswords = ["password", "123456", "letmein", "qwerty", "1234ABCD"];
    if (commonPasswords.includes(password.toLowerCase())) {
        return "Password is too common. Choose something less predictable.";
    }

    const sequentialPatterns = ["123", "abc", "xyz", "asdf"];
    for (const pattern of sequentialPatterns) {
        if (password.toLowerCase().includes(pattern)) {
            return "Password should not contain sequential characters like '123' or 'abc'.";
        }
    }

    let score = 0;
    if (password.length >= 10) {
        score += 1;
    }
    if (/[A-Z]/.test(password) && /[a-z]/.test(password)) {
        score += 1;
    }
    if (/\d/.test(password)) {
        score += 1;
    }
    if (/[!@#$%^&*()_+]/.test(password)) {
        score += 1;
    }

    let strength = "Weak";
    if (score === 2) {
        strength = "Medium";
    } else if (score >= 3) {
        strength = "Strong";
    }

    password = password.replace(/Hello/g, "Hi");
    const words = password.split(" ");
    const customizedPassword = words.join("-");

    return `Password is valid and ${strength}!`;
}

// Example usage
const password = prompt("Please input password: ");
console.log(pswd(password));



#python example - passwork validator
import re

def password_validator(password):
    if len(password) < 8:
        return "Password too short. Must be at least 8 characters."
    
    if password == password.lower() or password == password.upper():
        return "Password must contain both uppercase and lowercase letters."
    
    if not any(char.isdigit() for char in password):
        return "Password must contain at least one number."
    
    if not re.search(r"[!@#$%^&*()_+]", password):
        return "Password must contain at least one special character (e.g. !, @, #, etc.)"
    
    common_passwords = ["password", "123456", "letmein", "qwerty"]
    if password.lower() in common_passwords:
        return "Password is too common. Choose something less predictable."
    
    sequential_patterns = ["123", "abc", "xyz"]
    for pattern in sequential_patterns:
        if pattern in password.lower():
            return "Password should not contain sequential characters like '123' or 'abc'."
    
    score = 0
    if len(password) >= 10:
        score += 1
    if re.search(r"[A-Z]", password) and re.search(r"[a-z]", password):
        score += 1
    if re.search(r"\d", password):
        score += 1
    if re.search(r"[!@#$%^&*()_+]", password):
        score += 1

    strength = "Weak"
    if score == 2:
        strength = "Medium"
    elif score >= 3:
        strength = "Strong"

    password = password.replace("Hello", "Hi")
    words = password.split(" ")
    customized_password = "-".join(words)

    return f"Password is valid and {strength}!"

# Example usage
password = input("Please input password: ")
print(password_validator(password))