-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot13.js
27 lines (22 loc) · 912 Bytes
/
rot13.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
/*
Rot13 in Javascript
Encodes the string using Rot13. Rot13 works by replacing each upper and lower case letters with the letter 13 positions ahead or behind it in the alphabet.
*/
var s="Fraq hf gur pbqr lbh hfrq gb qrpbqr guvf zrffntr";
var decodedArray=[];
function incrementChar(c) {
return String.fromCharCode(c.charCodeAt(0) + 13);
}
function decrementChar(c) {
return String.fromCharCode(c.charCodeAt(0) - 13);
}
for (var i = 0; i < s.length; i++) {
var c = s.charAt(i);
if (c >= 'a' && c <= 'm'){c=incrementChar(c);}
else if (c >= 'A' && c <= 'M'){ c=incrementChar(c);}
else if (c >= 'n' && c <= 'z'){ c=decrementChar(c);}
else if (c >= 'N' && c <= 'Z'){ c=decrementChar(c);}
decodedArray.push(c);
}
decodedArray=decodedArray.join().replace(/,/g, '').toString();
console.log(decodedArray);