-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
229 lines (214 loc) · 4.78 KB
/
index.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//Current line
var CurrentId = undefined;
var inputValues = [];
const inputPrompts = [
"Type your message:",
"Type the shift number:",
"Type 'yes' if you want to go again. Otherwise type 'no'.",
];
logo = `
,adPPYba, ,adPPYYba, ,adPPYba, ,adPPYba, ,adPPYYba, 8b,dPPYba,
a8" "" "" 'Y8 a8P_____88 I8[ "" "" 'Y8 88P' "Y8
8b ,adPPPPP88 8PP""""""" '"Y8ba, ,adPPPPP88 88
"8a, ,aa 88, ,88 "8b, ,aa aa ]8I 88, ,88 88
'"Ybbd8"' '"8bbdP"Y8 '"Ybbd8"' '"YbbdP"' '"8bbdP"Y8 88
88 88
"" 88
88
,adPPYba, 88 8b,dPPYba, 88,dPPYba, ,adPPYba, 8b,dPPYba,
a8" "" 88 88P' "8a 88P' "8a a8P_____88 88P' "Y8
8b 88 88 d8 88 88 8PP""""""" 88
"8a, ,aa 88 88b, ,a8" 88 88 "8b, ,aa 88
'"Ybbd8"' 88 88'YbbdP"' 88 88 '"Ybbd8"' 88
88
88
`;
const 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",
"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",
"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",
];
//Click Run
$(document).ready(function () {
$("#run-button").click(function () {
$("#Content").empty();
NewLine(logo, false);
restart();
});
});
function restart() {
inputValues = [];
NewLine("Type 'encode' to encrypt, type 'decode' to decrypt:", true);
}
//Enter button
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
if (x === 13 || x == 13) {
var consoleLine = $("#" + CurrentId + " input").val();
inputValues.push({ id: CurrentId, val: consoleLine.toLowerCase() });
if (inputValues.length == 3) {
NewLine(
caesar(
inputValues[0].val,
inputValues[1].val,
Number(inputValues[2].val)
),
false
);
// NewLine(inputPrompts[2], true);
}
if (inputValues.length > inputPrompts.length) {
if (inputValues[3].val == "yes") {
restart();
return;
} else {
NewLine("Goodbye", false);
return;
}
}
$(".console-carrot").remove();
if (inputValues.length <= 3) {
NewLine(inputPrompts[inputValues.length - 1], true);
}
}
});
$(document).on("keydown", function (e) {
var x = event.which || event.keyCode;
var line = $("#" + CurrentId + " input");
var length = line.val().length;
if (x != 8) {
line.attr("size", 1 + length);
} else {
line.attr("size", length * 0.95);
}
if (length === 0) {
$("#" + CurrentId + " input").attr("size", "1");
}
});
$(document).on("click", function (e) {
$("#" + CurrentId + " input").focus();
});
//New line
function NewLine(text, isPrompt) {
$(".console-carrot").remove();
if (CurrentId !== undefined) {
$("#" + CurrentId + " input").prop("disabled", true);
}
CurrentId = "consoleInput-" + GenerateId();
if (isPrompt) {
$("#Content").append("<div>" + text + "</div>");
$("#Content").append(
'<div id="' +
CurrentId +
'">' +
'<input autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" type="text" class="terminal-input" /><div class="console-carrot"></div></div>'
);
$("#" + CurrentId + " input").focus();
$("#" + CurrentId + " input").attr("size", "1");
} else {
$("#Content").append('<div id="' + CurrentId + '">' + text + "</div>");
}
document.getElementById(CurrentId).scrollIntoView();
}
function caesar(cipher_direction, start_text, shift_amount) {
let end_text = "";
if (cipher_direction == "decode") {
shift_amount = shift_amount * -1 + 26;
}
for (char in start_text) {
if (alphabet.includes(start_text[char])) {
const position = alphabet.indexOf(start_text[char]);
const new_position = position + shift_amount;
end_text += alphabet[new_position];
} else {
end_text += start_text[char];
}
}
console.log(end_text);
return `Here's the ${cipher_direction}d result: ${end_text}`;
}
function GenerateId() {
return Math.random().toString(16).slice(2);
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}