-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathletter.js
More file actions
51 lines (42 loc) · 1.79 KB
/
letter.js
File metadata and controls
51 lines (42 loc) · 1.79 KB
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
function decrypt() {
// Get the message from the text box
var message = document.getElementById("message").value;
// Shift each letter of the message by 3
var decrypted = "";
for (var i = 0; i < message.length; i++) {
// Get the ASCII code of the current character
var c = message.charCodeAt(i);
// Shift the ASCII code by 3
// If the character is a letter (uppercase or lowercase), wrap around if necessary
if (c >= 65 && c <= 90) { // uppercase letter
c = (c - 65 + 3) % 26 + 65;
} else if (c >= 97 && c <= 122) { // lowercase letter
c = (c - 97 + 3) % 26 + 97;
}
// Append the encrypted character to the encrypted message
decrypted += String.fromCharCode(c);
}
// Update the HTML page with the encrypted message
document.getElementById("decrypted").innerHTML = decrypted;
}
function encrypt() {
// Get the message from the text box
var message2 = document.getElementById("message2").value;
// Shift each letter of the message by 3
var encrypted = "";
for (var i = 0; i < message2.length; i++) {
// Get the ASCII code of the current character
var c = message2.charCodeAt(i);
// Shift the ASCII code by 3
// If the character is a letter (uppercase or lowercase), wrap around if necessary
if (c >= 65 && c <= 90) { // uppercase letter
c = (c - 65 - 3 + 26) % 26 + 65;
} else if (c >= 97 && c <= 122) { // lowercase letter
c = (c - 97 - 3 + 26) % 26 + 97;
}
// Append the encrypted character to the encrypted message
encrypted += String.fromCharCode(c);
}
// Update the HTML page with the encrypted message
document.getElementById("encrypted").innerHTML = encrypted;
}