-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
79 lines (60 loc) · 2.46 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
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
//All commented console.logs are purely for debugging purposes
//Alphabet variable that's used for shifting the alphabet during encryption
var alphabet = ["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", "æ", "ø", "å"];
//The <p> tag housing the encrypted text
var encryptedField = document.getElementById("encryptedActual");
var textWarning = document.querySelector(".noTextWarning");
//The main function of the program, which is run when the "encrypt text" button is pressed and is responsible for the main encryption process
function recieveMessage() {
forShifting = alphabet.slice(0);
var assignedVariable = document.getElementById("choppingBlock").value;
var assignedNumber = document.getElementById("numero").value;
if (assignedVariable == ""){
textWarning.innerText = "You need to write something!";
console.error("No encryption variable detected, program terminated.");
}
else{
textWarning.innerText = "";
}
var output = []
//console.log(assignedVariable);
//Converts the user string into lowercase and then logs the result
let simplified = assignedVariable.toLowerCase();
//console.log(simplified);
if (simplified == "turing" || simplified == "alan turing"){
document.location.href = "turing.html";
}
//Shifts the alphabet as many times as specified by the user
for (let i = 0; i < assignedNumber; i++) {
forShifting.push(forShifting.shift());
}
//console.log(forShifting);
forShifting = forShifting.toString();
//console.log(forShifting);
forShifting = forShifting.replaceAll(',', '');
//console.log(forShifting);
for (i = 0; i < simplified.length; i++) {
var index = alphabet.indexOf(simplified[i]);
var letter = forShifting.charAt(index);
//console.log(letter);
output.push(letter);
//console.log(output);
}
//Replaces the different occurences of commas with empty spaces
output = output.toString();
//console.log(output);
output = output.replaceAll(',,,', ', ')
//console.log(output);
output = output.replaceAll(',,', ', ')
//console.log(output);
output = output.replaceAll(',', '')
//console.log(output);
//Applies and shows the encrypted text on the page
encryptedField.innerHTML = output;
}
//Clears the forms, update to clear encrypted text as well
function clearLabel() {
document.getElementById("myForm").reset();
encryptedField.innerHTML = "";
textWarning.innerText = ""
};