Code Lab

Password length from 20 to 40

EasyBasics5 min

Write a function isValidPassword(password) that checks the length of a password. The parameter password is a string.

  • Return true if the password has from 20 to 40 characters. Both 20 and 40 are valid.
  • Return false if it has fewer than 20 or more than 40 characters. The empty string '' has 0 characters, so it is not valid.
  • Every character counts, spaces too, also at the start and at the end. Do not remove anything.
  • Only the length matters, not which characters are used.

The result is a boolean: true or false. The passwords in the tests have only simple characters: letters, digits, spaces and signs like !.

Examples

isValidPassword('MySecretPassword2024')

Returns true

20 characters: the shortest valid password.
isValidPassword('qwerty')

Returns false

6 characters: too short.
isValidPassword('  my long password  ')

Returns true

Spaces count too: 20 characters.

Asked at EPAM · Source

Loading the editor...
  1. InputisValidPassword('MySecretPassword2024')
    Expectedtrue
  2. InputisValidPassword('correct horse battery staple')
    Expectedtrue
  3. InputisValidPassword('qwerty')
    Expectedfalse
  4. InputisValidPassword('this password is way too long to be accepted')
    Expectedfalse
  5. InputisValidPassword('my-password.is_long, ok?')
    Expectedtrue

Hidden tests run on Submit.