Popcorn Homework 3.4


// Define a string
let myString = "Hello, World!";

// Get the length of the string
let stringLength = myString.length;

// Output the length
console.log("The length of the string is:", stringLength);

Homework 3.4



Python


import re

def validate_password(password):
    # Define the validation criteria
    min_length = 8
    has_uppercase = re.search(r'[A-Z]', password)
    has_lowercase = re.search(r'[a-z]', password)
    has_digit = re.search(r'\d', password)
    has_special_char = re.search(r'[!@#$%^&*(),.?":{}|<>]', password)

    # Check if the password meets all criteria
    if len(password) < min_length:
        return "Password must be at least 8 characters long."
    elif not has_uppercase:
        return "Password must contain at least one uppercase letter."
    elif not has_lowercase:
        return "Password must contain at least one lowercase letter."
    elif not has_digit:
        return "Password must contain at least one number."
    elif not has_special_char:
        return "Password must contain at least one special character."
    else:
        return "Password is valid!"

# Take password input from the user
password = input("Enter your password: ")

# Validate the password and print the result
result = validate_password(password)
print(result)


Javascript


document.getElementById('passwordForm').addEventListener('submit', function(e) {
    e.preventDefault(); // Prevent form submission
  
    const password = document.getElementById('password').value;
    const resultDiv = document.getElementById('validationResult');
  
    // Validation checks using simple string methods
    const minLength = password.length >= 8;
    const hasUppercase = password !== password.toLowerCase(); // Checks if there's at least one uppercase letter
    const hasLowercase = password !== password.toUpperCase(); // Checks if there's at least one lowercase letter
    const hasNumber = password.indexOf('0') !== -1 || password.indexOf('1') !== -1 || 
                      password.indexOf('2') !== -1 || password.indexOf('3') !== -1 || 
                      password.indexOf('4') !== -1 || password.indexOf('5') !== -1 || 
                      password.indexOf('6') !== -1 || password.indexOf('7') !== -1 || 
                      password.indexOf('8') !== -1 || password.indexOf('9') !== -1;
  
    if (minLength && hasUppercase && hasLowercase && hasNumber) {
      resultDiv.innerHTML = `<p class="valid">Password is valid!</p>`;
    } else {
      resultDiv.innerHTML = `<p class="invalid">Password is invalid.</p>`;
    }
  });