Guest User

Untitled

a guest
Jun 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // One of the simplest and most widely known ciphers is a Caesar cipher,
  2. // also known as a shift cipher. In a shift cipher the meanings of the
  3. // letters are shifted by some set amount.
  4.  
  5. // A common modern use is the ROT13 cipher, where the values of the
  6. // letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.
  7.  
  8. // Write a function which takes a ROT13 encoded string as input and
  9. // returns a decoded string.
  10.  
  11. // All letters will be uppercase. Do not transform any non-alphabetic
  12. // character (i.e. spaces, punctuation), but do pass them on.
  13.  
  14. function rot13(str) { // LBH QVQ VG!
  15. let maxAscii=90;
  16. let minAscii=65;
  17. let rotate=13;
  18.  
  19. return str.split('').map(letter => {
  20. let curLetter=letter.charCodeAt();
  21. if ((curLetter >= minAscii) && (curLetter <= maxAscii)) {
  22. curLetter-=13;
  23. if (curLetter<65) { curLetter+=26; }
  24. }
  25. return String.fromCharCode(curLetter);}).join("");
  26. }
  27.  
  28. // Change the inputs below to test
  29. rot13("SERR PBQR PNZC"); // FREE CODE CAMP
  30. rot13("SERR CVMMN!"); // FREE PIZZA!
  31. rot13("SERR YBIR?"); // FREE LOVE?
  32. rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT."); // THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
Add Comment
Please, Sign In to add comment