AreWe

PasswordValidator Brankov

Jun 13th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(password) {
  2.  
  3.     function passLength(password) {
  4.         if (password.length >= 6 && password.length <= 10) {
  5.             return true;
  6.         }
  7.  
  8.         console.log('Password must be between 6 and 10 characters');
  9.         return false;
  10.     }
  11.  
  12.     function content(password) {
  13.         for (let digitLetter of password) {
  14.             let asciiDigit = digitLetter.charCodeAt(0);
  15.  
  16.             let isSmallLetter = c => (asciiDigit >= 97 && asciiDigit <= 122);
  17.             let isCapsLetter = c => (asciiDigit >= 65 && asciiDigit <= 90);
  18.             let isNumber = c => (asciiDigit >= 48 && asciiDigit <= 57);
  19.  
  20.             if (!isNumber(asciiDigit) && !isCapsLetter(asciiDigit) && !isSmallLetter(asciiDigit)) {
  21.                 console.log('Password must consist only of letters and digits');
  22.                 return false;
  23.             }
  24.         }
  25.  
  26.         return true;
  27.     }
  28.  
  29.     function countDigits(password) {
  30.         let counter = 0;
  31.  
  32.         for(let char of password) {
  33.             if(!isNaN(char)) {
  34.                 counter++;
  35.             }
  36.         }
  37.  
  38.         if (counter >= 2) {
  39.             return true;
  40.         }
  41.  
  42.         console.log('Password must have at least 2 digits');
  43.         return false;
  44.     }
  45.  
  46.     if (passLength(password) & content(password) & countDigits(password)) {
  47.         console.log('Password is valid');
  48.     }
  49. }
Add Comment
Please, Sign In to add comment