-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
103 lines (85 loc) · 3.29 KB
/
script.js
File metadata and controls
103 lines (85 loc) · 3.29 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
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
96
97
98
99
100
101
102
103
function redirectToDevsPage() {
window.location.href = "devs.html";
}
function redirectToHomePage() {
window.location.href = "index.html";
}
function redirectToChoice() {
const title = document.querySelector('.shift-cipher-title');
const aboutText = document.querySelector('.about-text');
title.classList.add('fade-out');
aboutText.classList.add('fade-out');
setTimeout(() => {
window.location.href = 'choice.html';
}, 1000);
}
function redirectToEncode() {
window.location.href = "encode.html";
}
function redirectToDecode() {
window.location.href = "decode.html";
}
document.addEventListener("DOMContentLoaded", function() {
const fadeInElements = [
'.action-button',
'.instruction-text',
'.input-text-form',
'.integer-form',
'.display-message'
];
function addFadeInClass(selector) {
const elements = document.querySelectorAll(selector);
elements.forEach(element => {
element.classList.add('fade-in');
});
}
fadeInElements.forEach(selector => addFadeInClass(selector));
});
function caesarCipher(str, shift) {
return Array.from(str).map(char => {
const code = char.charCodeAt(0);
if (code >= 65 && code <= 90) {
return String.fromCharCode(((code - 65 + shift + 26) % 26) + 65);
} else if (code >= 97 && code <= 122) {
return String.fromCharCode(((code - 97 + shift + 26) % 26) + 97);
} else {
return char;
}
}).join('');
}
function updateOutputDecode() {
const inputText = document.getElementById('coded-message').value;
const shiftValue = parseInt(document.getElementById('shift-key-decode').value) || 0;
const decrypted = caesarCipher(inputText, -shiftValue);
document.getElementById('display-message').innerText = decrypted;
}
function updateOutputEncode() {
const inputText = document.getElementById('uncoded-message').value;
const shiftValue = parseInt(document.getElementById('shift-key-encode').value) || 0;
const encrypted = caesarCipher(inputText, shiftValue);
document.getElementById('display-message-encode').innerText = encrypted;
}
function showShiftKey() {
const shiftValue = parseInt(document.getElementById('shift-key-decode').value) || 0;
let negativeShiftValue;
let positiveShiftValue;
if (shiftValue > 0) {
negativeShiftValue = -shiftValue;
positiveShiftValue = 26 - shiftValue;
} else if (shiftValue < 0) {
negativeShiftValue = -shiftValue;
positiveShiftValue = 26 + shiftValue;
} else {
negativeShiftValue = 0;
positiveShiftValue = 0;
}
document.getElementById('k').innerText = `Negative Shift: ${negativeShiftValue}, Positive Shift: ${positiveShiftValue}`;
}
document.getElementById('coded-message').addEventListener('input', updateOutputDecode);
document.getElementById('shift-key-decode').addEventListener('input', function() {
updateOutputDecode();
showShiftKey();
});
// sweatyy sauna
document.getElementById('uncoded-message').addEventListener('input', updateOutputEncode);
document.getElementById('shift-key-encode').addEventListener('input', updateOutputEncode);