-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcaesars_cipher.js
39 lines (33 loc) · 1.11 KB
/
caesars_cipher.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
var cipherText;
function caesarsCipherCrypt(str, key) {
var i = 0, toUp = str.toUpperCase(), size = toUp.length, newStr = [];
for(; i < size; i += 1) {
var toCode = toUp[i].charCodeAt(0);
if(toCode < 65 || toCode > 90) {
newStr[i] = String.fromCharCode(toCode);
} else if(toCode < 78) {
newStr[i] = String.fromCharCode(toCode + key);
} else {
newStr[i] = String.fromCharCode(toCode - key);
}
}
cipherText = newStr.join('');
return cipherText;
}
caesarsCipherCrypt('HELLO', 10);
function caesarsCipherEncrypt(str) {
var i = 0, toUp = str.toUpperCase(), size = toUp.length, newStr = [];
for(; i < size; i += 1) {
var toCode = toUp[i].charCodeAt(0);
if(toCode < 65 || toCode > 90) {
newStr[i] = String.fromCharCode(toCode);
} else if(toCode < 78) {
newStr[i] = String.fromCharCode(toCode + 10);
} else {
newStr[i] = String.fromCharCode(toCode - 10);
}
}
cipherText = newStr.join('');
return cipherText;
}
caesarsCipherEncrypt(cipherText);