-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
88 lines (75 loc) · 2.39 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
80
81
82
83
84
85
86
87
88
let encryptedMessage = "";
let decryptedMessage = "";
const alph = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
function copyText() {
const getResults = document.getElementById("result").value;
navigator.clipboard.writeText(getResults).then(
() => {
console.log("Text copied to clipboard");
},
() => {
console.error("Failed to copy text to clipboard");
}
);
}
function inputValidate(text) {
const isValidInput = /^[a-zA-Z\s]*$/.test(text);
if (text.length > 50 || text.length < 1) {
alert("Por favor, insira um texto com menos de 1 e 150 caracteres.");
return false;
}
if (!isValidInput) {
alert("Por favor, digite apenas letras e espaços.");
return false;
}
return true;
}
function decryptText() {
const getEntrada = encryptedMessage;
const getResults = document.getElementById("result");
// Limpar a variável ao clicar pela segunda vez
if (decryptedMessage) {
decryptedMessage = "";
}
for (const letter of getEntrada) {
if (letter === "a") {
decryptedMessage = decryptedMessage.concat("", "z");
} else if (letter === "A") {
decryptedMessage = decryptedMessage.concat("", "Z");
} else if (letter === " ") {
decryptedMessage = decryptedMessage.concat("", " ");
} else {
decryptedMessage = decryptedMessage.concat(
"",
alph[alph.indexOf(letter) - 1]
);
}
}
getResults.value = `${decryptedMessage}`;
}
function encryptText() {
const getEntrada = document.getElementById("entrada").value;
const getResults = document.getElementById("result");
const text = getEntrada.trim();
if (!inputValidate(text)) {
return;
}
// Limpar a variável ao clicar pela segunda vez
if (encryptedMessage) {
encryptedMessage = "";
}
for (const letter of text) {
if (letter === "z") {
encryptedMessage += "a";
} else if (letter === "Z") {
encryptedMessage += "A";
} else if (letter === " ") {
encryptedMessage += " ";
} else {
const index = alph.indexOf(letter);
encryptedMessage += alph[index + 1];
}
}
getResults.value = encryptedMessage;
document.getElementById("entrada").value = "";
}