2018-09-30 23:01:58 +01:00
---
id: 56533eb9ac21ba0edf2244e2
title: Caesars Cipher
challengeType: 5
2019-07-31 11:32:23 -07:00
forumTopicId: 16003
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-10-27 15:45:37 -01:00
One of the simplest and most widely known < dfn > ciphers< / dfn > is a < dfn > Caesar cipher< / dfn > , also known as a < dfn > shift cipher< / dfn > . In a shift cipher the meanings of the letters are shifted by some set amount.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
A common modern use is the [ROT13 ](https://en.wikipedia.org/wiki/ROT13 ) cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
Write a function which takes a [ROT13 ](https://en.wikipedia.org/wiki/ROT13 ) encoded string as input and returns a decoded string.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`rot13("SERR PBQR PNZC")` should decode to `FREE CODE CAMP`
```js
assert(rot13('SERR PBQR PNZC') === 'FREE CODE CAMP');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`rot13("SERR CVMMN!")` should decode to `FREE PIZZA!`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(rot13('SERR CVMMN!') === 'FREE PIZZA!');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`rot13("SERR YBIR?")` should decode to `FREE LOVE?`
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(rot13('SERR YBIR?') === 'FREE LOVE?');
```
2018-10-08 01:01:53 +01:00
2020-11-27 19:02:05 +01:00
`rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.")` should decode to `THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.`
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(
rot13('GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.') ===
'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.'
);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function rot13(str) {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
return str;
}
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
rot13("SERR PBQR PNZC");
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
var lookup = {
'A': 'N','B': 'O','C': 'P','D': 'Q',
'E': 'R','F': 'S','G': 'T','H': 'U',
'I': 'V','J': 'W','K': 'X','L': 'Y',
'M': 'Z','N': 'A','O': 'B','P': 'C',
'Q': 'D','R': 'E','S': 'F','T': 'G',
'U': 'H','V': 'I','W': 'J','X': 'K',
2018-10-08 01:01:53 +01:00
'Y': 'L','Z': 'M'
2018-09-30 23:01:58 +01:00
};
function rot13(encodedStr) {
var codeArr = encodedStr.split(""); // String to Array
var decodedArr = []; // Your Result goes here
// Only change code below this line
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
decodedArr = codeArr.map(function(letter) {
if(lookup.hasOwnProperty(letter)) {
letter = lookup[letter];
}
return letter;
});
// Only change code above this line
return decodedArr.join(""); // Array to String
}
```