89 lines
2.5 KiB
Markdown
89 lines
2.5 KiB
Markdown
|
---
|
||
|
id: 56533eb9ac21ba0edf2244e2
|
||
|
title: Build a Caesars Cipher
|
||
|
challengeType: 5
|
||
|
forumTopicId: 16003
|
||
|
dashedName: build-a-caesars-cipher
|
||
|
---
|
||
|
|
||
|
# --description--
|
||
|
|
||
|
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.
|
||
|
|
||
|
A common modern use is the <a href="https://www.freecodecamp.org/news/how-to-code-the-caesar-cipher-an-introduction-to-basic-encryption-3bf77b4e19f7/" target="_blank" rel="noopener noreferrer nofollow">ROT13</a> 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 <a href="https://www.freecodecamp.org/news/how-to-code-the-caesar-cipher-an-introduction-to-basic-encryption-3bf77b4e19f7/" target="_blank" rel="noopener noreferrer nofollow">ROT13</a> 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.
|
||
|
|
||
|
# --hints--
|
||
|
|
||
|
`rot13("SERR PBQR PNZC")` should decode to the string `FREE CODE CAMP`
|
||
|
|
||
|
```js
|
||
|
assert(rot13('SERR PBQR PNZC') === 'FREE CODE CAMP');
|
||
|
```
|
||
|
|
||
|
`rot13("SERR CVMMN!")` should decode to the string `FREE PIZZA!`
|
||
|
|
||
|
```js
|
||
|
assert(rot13('SERR CVMMN!') === 'FREE PIZZA!');
|
||
|
```
|
||
|
|
||
|
`rot13("SERR YBIR?")` should decode to the string `FREE LOVE?`
|
||
|
|
||
|
```js
|
||
|
assert(rot13('SERR YBIR?') === 'FREE LOVE?');
|
||
|
```
|
||
|
|
||
|
`rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.")` should decode to the string `THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.`
|
||
|
|
||
|
```js
|
||
|
assert(
|
||
|
rot13('GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.') ===
|
||
|
'THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.'
|
||
|
);
|
||
|
```
|
||
|
|
||
|
# --seed--
|
||
|
|
||
|
## --seed-contents--
|
||
|
|
||
|
```js
|
||
|
function rot13(str) {
|
||
|
return str;
|
||
|
}
|
||
|
|
||
|
rot13("SERR PBQR PNZC");
|
||
|
```
|
||
|
|
||
|
# --solutions--
|
||
|
|
||
|
```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',
|
||
|
'Y': 'L','Z': 'M'
|
||
|
};
|
||
|
|
||
|
function rot13(encodedStr) {
|
||
|
var codeArr = encodedStr.split(""); // String to Array
|
||
|
var decodedArr = []; // Your Result goes here
|
||
|
// Only change code below this line
|
||
|
|
||
|
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
|
||
|
}
|
||
|
```
|