-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesarsCipher.js
More file actions
34 lines (30 loc) · 1.24 KB
/
caesarsCipher.js
File metadata and controls
34 lines (30 loc) · 1.24 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
// One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
//
// A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
//
// Write a function which takes a ROT13 encoded string as input and returns a decoded string.
//
// All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
function rot13(str) {
var strLength = str.length;
var count = 0;
var result = "";
var check;
while(count < strLength) {
check = 0;
if (str.charCodeAt(count) < 65 || str.charCodeAt(count) > 90) {
result = result.concat(str[count]);
} else {
check = str.charCodeAt(count) + 13;
if (check > 90) {
check = str.charCodeAt(count) - 13;
result = result.concat(String.fromCharCode(check));
} else {
result = result.concat(String.fromCharCode(check));
}
}
count++;
}
return result;
}
console.log(rot13("SERR PBQR PNZC")); // "FREE CODE CAMP"