判断是否是强密码还是弱密码

kingcwt2023-09-23前端算法 基础数据结构 面试题

要求

To ensure user safety, the website will perform password strength validation on the password set by the user during registration.

A strong password MUST follow all the rules

- The same character cannot appear consecutively three times.
- The length of the password must between 8 and 22 characters.
- The password must contain at least one lowercase letter, one uppercase
letter, and one number

Return "strong" if the given password is strong, otherwise return "weak". Be aware, please DO NOT return true or false.

题解

const solution3 = (password) => {
  if (password.length >= 8 && password.length <= 22) {
    if (
      /[a-z]/.test(password) &&
      /[A-Z]/.test(password) &&
      /\d/.test(password)
    ) {
      for (let i = 2; i < password.length; i++) {
        if (
          password[i] === password[i - 1] &&
          password[i] === password[i - 2]
        ) {
          return "weak";
        }
      }

      return "strong";
    }
  }

  return "weak";
};

// let s1='1234567890Abcd',s2='1234567890aaaa',s3='1234567890abcd',s4='12345678',s5='abababababababababaaa',s6='1010101010aaB10101010'
// console.log(solution(s6))
Last Updated 10/16/2023, 7:06:22 AM
What do you think?
  • 0
  • 0
  • 0
  • 0
  • 0
  • 0
Comments
  • Latest
  • Oldest
  • Hottest
Powered by Waline v2.15.8