1

I have written a program to validate password input based on different criteria and if those criteria are met, 25% is added at each point and the total(passwordStrength) is output at the end. This is the implementation:

let passwordStrength = 0;
function checkPassword(password) {
    if (typeof password !== 'string' || password.trim() === ''){
        return 'Only strings are allowed';
    }
    let trimmedPassword = password.trim();
    let regex1 = /[a-z]/;
    let regex2 = /[A-Z]/;
    let regex3 = /[0-9]/;
    let regex4 = /[$@#&!]/;

    if (regex1.test(trimmedPassword)){
        passwordStrength += 25;
    }
    if (regex2.test(trimmedPassword)){
        passwordStrength += 25;
    }
    if (regex3.test(trimmedPassword)){
        passwordStrength += 25;
    }
    if (regex4.test(trimmedPassword)){
        passwordStrength += 25;
    }
    if (trimmedPassword.length < 6 || trimmedPassword.length > 12){
        return 'Password can not be less than 6 or greater than 12 characters';
    }
    return passwordStrength += '%';
}
console.log(checkPassword('@manGaLa'));// returns 75%

Now, I want to add the respective statements accompanying the various strengths like so:

if passwordStrength === 25%, return poor,
if passwordStrength === 50%; return weak,
if passwordStrength === 75%; return medium,
if passwordStrength === 100%; return strong.

How do I add this without making the programme too long?

danoseun
  • 69
  • 2
  • 9
  • You should read [Reference - Password Validation](https://stackoverflow.com/questions/48345922/reference-password-validation) – ctwheels May 30 '18 at 15:32

1 Answers1

2

If you find yourself performing the same operation multiple times, you should consider using some kind of loop. Here, using reduce is beneficial so you don't have to maintain a variable outside the function.

function checkPassword(password) {
  if (typeof password !== 'string' || password.trim() === '') {
    return 'Only strings are allowed';
  }
  let trimmedPassword = password.trim();

  if (trimmedPassword.length < 6 || trimmedPassword.length > 12) {
    return 'Password can not be less than 6 or greater than 12 characters';
  }

  return [/[a-z]/, /[A-Z]/, /[0-9]/, /[$@#&!]/].reduce((total, regex) => {
    return regex.test(trimmedPassword) ? total + 25 : total;
  }, 0) + '%';
}

console.log(checkPassword('@manGaLa'));

If you want to include some additional text about password strength, you should consider if you need both the percent and the text. Do you only need them both together? Do you need them separately? Since you are returning strings, you can use an object as a dictionary:

const percentToHint = {
  '25%': 'poor',
  '50%': 'weak',
  '75%': 'medium',
  '100%': 'strong'
}

percentToHint[yourReturnValue] // => result

but this might only make sense if you need to keep this text separate from your checkPassword function.

Damon
  • 4,216
  • 2
  • 17
  • 27
  • thanks for the optimization and extra tips. I actually thought up doing the checkPassword and passwordStrength separately too but I am just curious about an implementation that brings them together. – danoseun May 26 '18 at 11:58