Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. function solve(password){
  2.  
  3. //Password validation
  4. let isDigit = (asciiCode) => asciiCode>= 48 && asciiCode<= 57;
  5.  
  6. let validLenght = hasValidLenght();
  7. let validContent = hasValidContent();
  8. let validDigit = hasAtLeastTwoDigits();
  9.  
  10. if (!validLenght) {
  11. console.log("Password must be between 6 and 10 characters");
  12. }
  13. if (!validContent) {
  14. console.log("Password must consist only of letters and digits");
  15. }
  16. if (!validDigit) {
  17. console.log("Password must have at least 2 digits");
  18. }
  19. if (validLenght && validContent && validDigit) {
  20. console.log("Password is valid");
  21. }
  22. function hasValidLenght(){
  23. return password.lenght >= 6 && password.lenght <= 10;
  24. }
  25. function hasValidContent(){
  26. let lowerCasePass = password.toLowerCase();
  27. let isLetter= (asciiCode) => asciiCode >=97 && asciiCode<=122;
  28.  
  29. for (let i = 0; i < lowerCasePass.length; i++) {
  30. let ascii = lowerCasePass.charCodeAt(i);
  31. if (isLetter(ascii) || isDigit(ascii)) {
  32. continue;
  33. }
  34. return false;
  35. }
  36. return true;
  37. }
  38. function hasAtLeastTwoDigits(){
  39. let digitsCount = 0;
  40. for (let i = 0; i < password.length; i++) {
  41. let ascii = password .charCodeAt(i);
  42. let asciiIsDigit = isDigit(ascii);
  43.  
  44. if (asciiIsDigit) {
  45. digitsCount ++;
  46. }
  47. }
  48. return digitsCount >=2;
  49. }
  50. }
  51. solve('MyPass123')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement