-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
39 lines (34 loc) · 1.52 KB
/
script.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
const alphabetArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
function encryptData() {
const inputText = document.getElementById('inputText').value.toLowerCase();
const shiftAmount = parseInt(document.getElementById('inputShift').value);
let outputText = '';
for (let i = 0; i < inputText.length; i++) {
if (alphabetArray.includes(inputText[i])) {
const currentIndex = alphabetArray.indexOf(inputText[i]);
const newIndex = (currentIndex + shiftAmount) % alphabetArray.length;
outputText += alphabetArray[newIndex];
} else {
outputText += inputText[i];
}
}
document.getElementById('outputText').innerHTML = outputText;
}
function decryptData() {
const inputText = document.getElementById('inputText').value.toLowerCase();
const shiftAmount = parseInt(document.getElementById('inputShift').value);
let outputText = '';
for (let i = 0; i < inputText.length; i++) {
if (alphabetArray.includes(inputText[i])) {
const currentIndex = alphabetArray.indexOf(inputText[i]);
let newIndex = (currentIndex - shiftAmount) % alphabetArray.length;
if (newIndex < 0) {
newIndex += alphabetArray.length;
}
outputText += alphabetArray[newIndex];
} else {
outputText += inputText[i];
}
}
document.getElementById('outputText').innerHTML = outputText;
}