-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesars_Cipher.js
More file actions
35 lines (33 loc) · 1.04 KB
/
Caesars_Cipher.js
File metadata and controls
35 lines (33 loc) · 1.04 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
function rot13 (str) {
return str.replace(/[A-Z]/g, function (x) {
return x.charCodeAt(0) > 77 ? String.fromCharCode(x.charCodeAt(0) - 13) : String.fromCharCode(x.charCodeAt(0) + 13);
});
}
/* Refactored from my old code on Jan 18, 2016
function rot13(str) {
var decodedArr = [];
for (var i = 0; i < str.length; i++) {
if (str.charCodeAt(i) >= 65 & str.charCodeAt(i) <= 90)
{
if (str.charCodeAt(i) > 77)
{
decodedArr.push(String.fromCharCode(str.charCodeAt(i) - 13));
}
else
{
decodedArr.push(String.fromCharCode(str.charCodeAt(i) + 13));
}
}
else
{
decodedArr.push(String.fromCharCode(str.charCodeAt(i)));
}
}
return decodedArr.join("");
}
*/
rot13("SERR PBQR PNZC"); // Should decode to "FREE CODE CAMP"
rot13("SERR CVMMN!"); // Should decode to "FREE PIZZA!"
rot13("SERR YBIR?"); // Should decode to "FREE LOVE?"
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");
// Should decode to "THE QUICK BROWN DOG JUMPED OVER THE LAZY FOX."