Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve (password) {
- let isValid = true;
- if (password.length < 6 || password.length > 10) {
- console.log(`Password must be between 6 and 10 characters`);
- isValid = false;
- }
- if (countOfInvalidChars(password) > 0) {
- console.log(`Password must consist only of letters and digits`);
- isValid = false;
- }
- if (countDigits(password) < 2) {
- console.log(`Password must have at least 2 digits`);
- isValid = false;
- }
- if(isValid) {
- console.log(`Password is valid`);
- }
- function countOfInvalidChars(password) {
- let alphanumericArray = [];
- let invalidChars = 0;
- for (let i = 48; i <= 57; i++) {
- alphanumericArray.push(String.fromCharCode(i));
- }
- for (let i = 65; i <= 90; i++) {
- alphanumericArray.push(String.fromCharCode(i));
- }
- for (let i = 97; i <= 122; i++) {
- alphanumericArray.push(String.fromCharCode(i));
- }
- for (let i = 0; i < password.length; i++) {
- let char = password[i];
- if (!alphanumericArray.includes(char)) {
- invalidChars++;
- }
- }
- return invalidChars;
- }
- function countDigits(password) {
- let count = 0;
- for (let i = 0; i < password.length; i++) {
- let char = password[i];
- let code = char.charCodeAt(0);
- if (code >= 48 && code <= 57) {
- count++;
- }
- }
- return count;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement