-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.js
More file actions
51 lines (43 loc) · 1.05 KB
/
solution.js
File metadata and controls
51 lines (43 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* eslint-disable no-restricted-syntax */
const fs = require('fs');
function isValidPassword(password) {
let hasLetters = false;
let lastDigit = -1;
let lastChar = '';
for (const char of password) {
if (/\d/.test(char)) {
if (hasLetters) return false;
const digit = Number(char);
if (digit < lastDigit) return false;
lastDigit = digit;
} else if (/[a-z]/.test(char)) {
hasLetters = true;
if (char < lastChar) return false;
lastChar = char;
} else {
return false;
}
}
return true;
}
function countValidInvalidPasswords(filePath) {
const data = fs.readFileSync(filePath, 'utf8');
const passwords = data
.split('\n')
.map((pwd) => pwd.trim())
.filter(Boolean);
let validCount = 0;
let invalidCount = 0;
passwords.forEach((password) => {
if (isValidPassword(password)) {
validCount++;
} else {
invalidCount++;
}
});
return `submit ${validCount}true${invalidCount}false`;
}
module.exports = {
isValidPassword,
countValidInvalidPasswords,
};