-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (66 loc) · 2.16 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//Multiplier Program by Vector
const modifiers = require("./ModifierList.js");
const fs = require("fs");
fs.readFile("input.txt", "utf8", (err, data) => {
if (err) {
console.error(err);
return;
}
const lines = data.split("\n");
const newContent = lines.map((line) => {
// Check if the line is a comment line, indicated by a "#" character at the start
const commentIndex = line.indexOf("#");
if (commentIndex === 0) {
// The line is a comment line, return an empty string
return "";
} else if (commentIndex > 0) {
// The line contains a comment, remove the comment and trim the line
line = line.substring(0, commentIndex).trim();
} else {
// The line does not contain a comment, trim the line
line = line.trim();
}
// Check if the line is an empty line
if (line === "") {
return "";
}
// Check if the line contains an allowed word
const containsAllowedWord = modifiers.allowedWords.some((word) => {
return line.indexOf(word) !== -1;
});
const containsBannedWord = modifiers.bannedWords.some((word) => {
return line.indexOf(word) !== -1;
});
if (containsBannedWord) {
// The line contains a banned word, return the original line
return line;
}
if (containsAllowedWord) {
// The line contains an allowed word, update the line
const words = line.split(" ");
const updatedWords = words.map((word) => {
if (!isNaN(word)) {
const parsedNumber = parseFloat(word) * 3;
const decimalLength = word.split(".")[1]?.length || 0;
if (decimalLength <= 3) {
return parsedNumber.toFixed(3) + " #Multipled by app.js";
} else {
return parsedNumber + " #Multipled1 by app.js";
}
}
return word;
});
return updatedWords.join(" ");
}
// The line does not contain an allowed word, return the original line
return line;
});
const updatedContent = newContent.join("\n");
fs.writeFile("output.txt", updatedContent, (err) => {
if (err) {
console.error(err);
return;
}
console.log("Output Updated");
});
});