-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02.js
63 lines (53 loc) · 1.28 KB
/
02.js
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
52
53
54
55
56
57
58
59
60
61
62
63
const fs = require("fs").promises;
async function readFileAndProcess() {
try {
const data = await fs.readFile("log.txt", "utf8");
const formattedData = data.split("\n");
return formattedData;
} catch (err) {
console.error(err);
}
}
(async () => {
const formattedData = await readFileAndProcess();
let validAttempts = 0;
let invalidAttempts = 0;
for (const attempt of formattedData) {
if (validateString(attempt)) {
validAttempts++;
} else {
invalidAttempts++;
}
}
console.log(`submit ${validAttempts}true${invalidAttempts}false`);
})();
function isDigit(char) {
return /^[0-9]$/.test(char);
}
function isLowercaseLetter(char) {
return /^[a-z]$/.test(char);
}
function validateString(input) {
let seenLetter = false;
let prevLetter = "";
let prevDigit = -1;
for (let i = 0; i < input.length; i++) {
const char = input[i];
if (isDigit(char)) {
const currentDigit = parseInt(char);
if (seenLetter || currentDigit < prevDigit) {
return false;
}
prevDigit = currentDigit;
} else if (isLowercaseLetter(char)) {
if (seenLetter && char < prevLetter) {
return false;
}
prevLetter = char;
seenLetter = true;
} else {
return false;
}
}
return true;
}