Skip to content

Instantly share code, notes, and snippets.

@tianmingzuo
Created October 14, 2019 16:31
Show Gist options
  • Select an option

  • Save tianmingzuo/1c5dc0791a27a9b9103157ed8b2f2f4d to your computer and use it in GitHub Desktop.

Select an option

Save tianmingzuo/1c5dc0791a27a9b9103157ed8b2f2f4d to your computer and use it in GitHub Desktop.
Caesars Cipher: One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipherthe 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 w…
function rot13(str) { // LBH QVQ VG!
let newStr = '';
for(let i=0; i<str.length; i++){
let asciiCode = str.charCodeAt(i);
if(asciiCode>=78 && asciiCode <=90){
asciiCode = asciiCode - 13;
}else if(asciiCode <78 && asciiCode >= 65){
asciiCode = asciiCode + 13;
}
newStr += String.fromCharCode(asciiCode);
}
return newStr;
}
// Change the inputs below to test
//rot13("SERR PBQR PNZC");
//rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");
rot13("SERR CVMMN!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment