JavaScript Algorithms and Data Structures Projects: Caesars CipherPassed
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.
const rot13 = str =>
String.fromCharCode(...str.split('').map(l =>
/\w/.test(l) ?
(l.charCodeAt(0) - 'A'.charCodeAt(0) + 13) % 26 + 'A'.charCodeAt(0)
:
l.charCodeAt(0)
));
[
rot13("SERR PBQR PNZC") ,
rot13("SERR CVMMN!") ,
rot13("SERR YBIR?") ,
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.")
].forEach(result => console.log(result));
FREE CODE CAMP
FREE PIZZA!
FREE LOVE?
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
No comments:
Post a Comment