Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(password){
- //Password validation
- let isDigit = (asciiCode) => asciiCode>= 48 && asciiCode<= 57;
- let validLenght = hasValidLenght();
- let validContent = hasValidContent();
- let validDigit = hasAtLeastTwoDigits();
- if (!validLenght) {
- console.log("Password must be between 6 and 10 characters");
- }
- if (!validContent) {
- console.log("Password must consist only of letters and digits");
- }
- if (!validDigit) {
- console.log("Password must have at least 2 digits");
- }
- if (validLenght && validContent && validDigit) {
- console.log("Password is valid");
- }
- function hasValidLenght(){
- return password.lenght >= 6 && password.lenght <= 10;
- }
- function hasValidContent(){
- let lowerCasePass = password.toLowerCase();
- let isLetter= (asciiCode) => asciiCode >=97 && asciiCode<=122;
- for (let i = 0; i < lowerCasePass.length; i++) {
- let ascii = lowerCasePass.charCodeAt(i);
- if (isLetter(ascii) || isDigit(ascii)) {
- continue;
- }
- return false;
- }
- return true;
- }
- function hasAtLeastTwoDigits(){
- let digitsCount = 0;
- for (let i = 0; i < password.length; i++) {
- let ascii = password .charCodeAt(i);
- let asciiIsDigit = isDigit(ascii);
- if (asciiIsDigit) {
- digitsCount ++;
- }
- }
- return digitsCount >=2;
- }
- }
- solve('MyPass123')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement