· 8 months ago · Mar 10, 2025, 03:05 PM
1function solve (password) {
2
3 let isValid = true;
4
5 if (password.length < 6 || password.length > 10) {
6 console.log(`Password must be between 6 and 10 characters`);
7 isValid = false;
8 }
9
10 if (countOfInvalidChars(password) > 0) {
11 console.log(`Password must consist only of letters and digits`);
12 isValid = false;
13 }
14
15
16 if (countDigits(password) < 2) {
17 console.log(`Password must have at least 2 digits`);
18 isValid = false;
19 }
20
21 if(isValid) {
22 console.log(`Password is valid`);
23 }
24
25
26 function countOfInvalidChars(password) {
27 let alphanumericArray = [];
28 let invalidChars = 0;
29
30
31 for (let i = 48; i <= 57; i++) {
32 alphanumericArray.push(String.fromCharCode(i));
33 }
34
35 for (let i = 65; i <= 90; i++) {
36 alphanumericArray.push(String.fromCharCode(i));
37 }
38
39 for (let i = 97; i <= 122; i++) {
40 alphanumericArray.push(String.fromCharCode(i));
41 }
42
43 for (let i = 0; i < password.length; i++) {
44 let char = password[i];
45
46 if (!alphanumericArray.includes(char)) {
47 invalidChars++;
48 }
49 }
50
51 return invalidChars;
52 }
53
54
55
56
57
58 function countDigits(password) {
59 let count = 0;
60
61 for (let i = 0; i < password.length; i++) {
62 let char = password[i];
63 let code = char.charCodeAt(0);
64
65 if (code >= 48 && code <= 57) {
66 count++;
67 }
68 }
69
70 return count;
71 }
72}
73
74
75
76