Advertisement
teofarov13

Untitled

Feb 5th, 2023
735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function passVall(password) {
  2.     //     Write a function that checks if a given password is valid. Password validations are:
  3.     // •  The length should be 6 - 10 characters (inclusive)
  4.    
  5.  
  6.     function length(password) {
  7.         let counter = 0;
  8.         for (let index = 0; index < password.length; index++) {
  9.             counter++
  10.         }
  11.         if (password.length >= 6 && password.length <= 10) {
  12.             return true
  13.         } else {
  14.             console.log(`Password must be between 6 and 10 characters`)
  15.             return false
  16.         }
  17.     }
  18.     function charCheck(password) {
  19.        
  20.         for (let index = 0; index < password.length; index++) {
  21.             let temp = password[index];
  22.             let tempChar = temp.charCodeAt(0);
  23.            
  24.             if ((tempChar >= 97 && tempChar <= 122) || (tempChar >= 65 && tempChar <= 90)
  25.             ||(tempChar >= 48 && tempChar <= 57)) {
  26.                
  27.             } else {
  28.                 console.log(`Password must consist only of letters and digits`);
  29.                 return false
  30.             }
  31.    
  32.         }
  33.     return true
  34.     }
  35.     function digitCheck(password) {
  36.         let digitCount = 0;
  37.         for (let index = 0; index < password.length; index++) {
  38.             let temp = password[index];
  39.             let tempChar = temp.charCodeAt(0);
  40.  
  41.  
  42.             if (tempChar >= 48 && tempChar <= 57) {
  43.                 temp = Number(temp);
  44.                 digitCount++
  45.             }
  46.         }
  47.         if (digitCount >= 2) {
  48.             return true
  49.         } else {
  50.             console.log("Password must have at least 2 digits");
  51.             return false
  52.         }
  53.     }
  54.  
  55.    
  56.    
  57.     let lengthCheck = length(password);
  58.     let char = charCheck(password);
  59.     let digitsCheck = digitCheck(password);
  60.    
  61.     if (lengthCheck === true && digitsCheck === true && char===true) {
  62.         console.log("Password is valid");
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement