miroLLL

Password validator

Jun 12th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solution(password) {
  2.  
  3.     let v1 = isLengthEnough(password);
  4.     let v2 = isAlphabetical(password);
  5.     let v3 = isContainsTwoDigits(password);
  6.  
  7.     if (!v1) {
  8.         console.log('Password must be between 6 and 10 characters');
  9.     };
  10.  
  11.     if (!v2) {
  12.         console.log('Password must consist only of letters and digits');
  13.     }
  14.  
  15.     if (!v3) {
  16.         console.log('Password must have at least 2 digits');
  17.     }
  18.  
  19.     if(v1 && v2 && v3) {
  20.         console.log('Password is valid')
  21.     }
  22.  
  23.     function isLengthEnough(pass) {
  24.         return pass.length >= 6 && pass.length <= 10;
  25.     }
  26.  
  27.     function isAlphabetical(pass) {
  28.  
  29.         let isLowerLetter = (c) => c >= 87 && c <= 122;
  30.         let isUpperLetter = (c) => c >= 65 && c <= 90;
  31.  
  32.         let isOk = true;
  33.  
  34.         for (let char of pass) {
  35.             let n = char.charCodeAt(0);
  36.  
  37.             if (!isDigit(n) && !isLowerLetter(n) && !isUpperLetter(n)) {
  38.                 isOk = false;
  39.                 break;
  40.             }
  41.         }
  42.  
  43.         return isOk;
  44.     }
  45.  
  46.     function isContainsTwoDigits(pass) {
  47.  
  48.         let counter = 0;
  49.         let isOk = false;
  50.  
  51.         for (let char of pass) {
  52.             let n = char.charCodeAt(0);
  53.  
  54.             if (isDigit(n)) {
  55.                 counter++;
  56.             }
  57.  
  58.             if (counter === 2) {
  59.                 isOk = true;
  60.                 break;
  61.             }
  62.         }
  63.  
  64.         return isOk;
  65.     }
  66.  
  67.     function isDigit(c) {
  68.         return c >= 48 && c <= 57;
  69.     }
  70. }
Add Comment
Please, Sign In to add comment