create a password with at least one uppercase letter in typescript

To create a password with at least one uppercase letter in TypeScript, you can use a regular expression to check if the password contains at least one uppercase character. Here's an example function that generates a random password with at least one uppercase letter:

index.ts
function generatePassword(): string {
  const uppercaseRegex = /[A-Z]/; // regular expression to match uppercase letters
  let password = ""; // initialize an empty password string
  while (!uppercaseRegex.test(password)) { // loop until the password contains at least one uppercase letter
    password = ""; // reset the password string
    const length = Math.floor(Math.random() * 10) + 8; // generate a random length between 8 and 17
    for (let i = 0; i < length; i++) {
      const randomChar = String.fromCharCode(Math.floor(Math.random() * 94) + 33); // generate a random character between ASCII 33 (!) and 126 (~)
      password += randomChar; // append the random character to the password string
    }
  }
  return password;
}
739 chars
14 lines

This function loops until the password string contains at least one uppercase letter. It generates a random length between 8 and 17 characters, and then generates random characters between ASCII 33 and 126 (inclusive) to fill the password string. Once the password string contains at least one uppercase letter, the function returns the password.

gistlibby LogSnag