Ivankooo1

Password Validator

Dec 21st, 2019
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(password){
  2.     let isDigit = (asciiCode) => asciiCode >= 48 && asciiCode <= 57;
  3.    
  4.     let validLength = hasValidLength();
  5.     let validContent = hasValidContent();
  6.     let validDigits = hasAtleastTwoDigits();
  7.  
  8.     if(!validLength){
  9.         console.log("Password must be between 6 and 10 characters")
  10.     }
  11.  
  12.     if(!validContent){
  13.         console.log("Password must consist only of letters and digits")
  14.     }
  15.    
  16.     if(!validDigits){
  17.         console.log("Password must have at least 2 digits");
  18.     }
  19.  
  20.     if(validLength && validContent && validDigits){
  21.         console.log("Password is valid.");
  22.     }
  23.  
  24.  
  25.   function hasValidLength(){
  26.       return password.length >= 6 && password.length <= 10;
  27.   }
  28.  
  29.  
  30.   function hasValidContent(){
  31.  
  32.       let lowerCase = password.toLowerCase();
  33.       let isLetter = (asciiCode) => asciiCode >= 97 && asciiCode <= 122;
  34.      
  35.       for(let i = 0; i < lowerCase.length;i++){
  36.  
  37.           let ascii = lowerCase.charCodeAt(i);
  38.           if(isLetter(ascii) || isDigit(ascii)){
  39.               continue;
  40.           }
  41.           return false;
  42.  
  43.       }
  44.       return true;
  45.   }
  46.  
  47.   function hasAtleastTwoDigits(){
  48.       let digitsCount = 0;
  49.       for(let i = 0;i < password.length;i++){
  50.           let ascii = password.charCodeAt(i);
  51.  
  52.           let asciiIsDigit = isDigit(ascii);
  53.  
  54.           if(asciiIsDigit){
  55.               digitsCount++;
  56.           }
  57.       }
  58.      return digitsCount >= 2;
  59.   }
  60. }
  61.  
  62. // solve('logIn');
  63. //solve('MyPass123');
  64. solve('Pa$s$s');
Advertisement
Add Comment
Please, Sign In to add comment