-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot13.js
32 lines (31 loc) · 1.05 KB
/
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
28
29
30
31
32
//ROT-13 Very basic Caesar cipher.
RotThirteen = function(input) {
var letters = ['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'];
//We transform the string into an array.
var result = input.split('').map(function(char){
var isInUpperCase = false;
//We check if the actual char is in upper case
if(char == char.toUpperCase()){
isInUpperCase = true;
var lowerCaseChar = char.toLowerCase();
//We save the actual character index
var index = letters.indexOf(lowerCaseChar);
}else{
var index = letters.indexOf(char);
}
//If the char was not found, it's possibly a special one char, so we just return it.
//Otherwise we substituted with the required char.
if(index == -1){
return char;
}
else{
if(isInUpperCase){
return letters[(index+13)%26].toUpperCase();
}else{
return letters[(index+13)%26];
}
}
});
//We return an string formed by the array.
return result.join('');
};