Advertisement
Lulunga

Password Validator

Jun 20th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  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. function hasValidLength(){
  24. return password.length>=6&&password.length<=10;
  25. }
  26. function hasValidContent(){
  27. let lowercasePass=password.toLowerCase();
  28. let isLetter = (asciiCode) =>asciiCode >= 97 && asciiCode <= 122;
  29.  
  30. for (let i = 0; i < lowercasePass.length; i++) {
  31. let ascii = lowercasePass.charCodeAt(i);
  32.  
  33. if (isLetter(ascii)||isDigit(ascii)) {
  34. continue;
  35. }
  36. return false;
  37. }
  38. return true;
  39. }
  40. function hasAtLeastTwoDigits(){
  41. let digitsCount=0;
  42. for (let i = 0; i < password.length; i++) {
  43. let ascii = password.charCodeAt(i);
  44.  
  45. let asciiIsDigit = isDigit(ascii);
  46.  
  47. if (asciiIsDigit) {
  48. digitsCount++;
  49. }
  50.  
  51. }
  52. return digitsCount>=2;
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement