generated from chalu/holiday-challenge-template
/
app.mjs
95 lines (84 loc) · 3 KB
/
app.mjs
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// DOM elements variables
const resetBtn = document.querySelector("#reset-btn"),
showRedact = document.querySelector(".show-result"),
submitBtn = document.querySelector("#submit-btn"),
clearBtn = document.querySelector("#clear-btn"),
totalWords = document.querySelector("#total-words"),
totalCharacters = document.querySelector("#total-characters"),
totalRedacted = document.querySelector("#total-redacted"),
totalTime = document.querySelector("#total-time");
function startApp() {
submitBtn.addEventListener("click", getRedact);
clearBtn.addEventListener("click", clearAll)
};
showRedact.textContent == (showRedact.style.display = "none")
// function to redact text on click
function getRedact(e) {
// getting the execution time
let start = performance.now();
// Input value variables
const textContent = document.querySelector("#text-area").value,
redactedWords = document.querySelector("#redacted-words").value,
redactSymbol = document.querySelector("#redact-sym").value,
textContentSplit = textContent.split(/[\s]|[.,]/),
totalTextChar = textContentSplit.join(""),
redactedWordSplit = redactedWords.split(" "),
totalRedactWords = [],
newTextContentSplit = [];
// Change redact words to lowercase
for (let word of textContentSplit) {
newTextContentSplit.push(word.toLowerCase())
}
// Find a match and replace with desired symbol
for (let item of redactedWordSplit) {
if (newTextContentSplit.includes(item.toLowerCase())) {
totalRedactWords.push(item)
const index = newTextContentSplit.findIndex((el) => el === item.toLowerCase())
newTextContentSplit[index] = new String(convertSymbol(item, redactSymbol))
}
}
// redaction results metrics
if (textContent === "") {
totalRedacted.textContent = 0
} else {
totalRedacted.textContent = totalRedactWords.length
}
showRedact.textContent = newTextContentSplit.join(" ");
if (showRedact.textContent !== ""){
showRedact.style.display = "block";
}else {
showRedact.style.display = "none";
}
totalWords.textContent = textContentSplit.length;
totalCharacters.textContent = totalTextChar.length;
let timeTaken = (performance.now() - start).toFixed(2);
totalTime.textContent = timeTaken + "ms";
console.log(timeTaken + "ms")
e.preventDefault();
}
// function to clear all inputs
function clearAll(e) {
document.querySelector(".redact-form").reset();
totalWords.textContent = "0";
totalCharacters.textContent = "0";
totalRedacted.textContent = "0";
totalTime.textContent = "0ms";
showRedact.textContent = "";
if (showRedact.textContent === "") {
showRedact.style.display = "none";
}
e.preventDefault();
}
// function to convert strings to symbols
function convertSymbol(name, symbol) {
let newItem = ""
for (let index = 0; index < name.length; index++) {
if (symbol == "") {
symbol = "@"
}
newItem += symbol
} return newItem
}
// ======= DO NOT EDIT ============== //
export default startApp;
// ======= EEND DO NOT EDIT ========= //