Skip to content

Instantly share code, notes, and snippets.

@jgresalfi
Last active November 24, 2022 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgresalfi/3fd39e0cf525980c115b4a34b9e0957f to your computer and use it in GitHub Desktop.
Save jgresalfi/3fd39e0cf525980c115b4a34b9e0957f to your computer and use it in GitHub Desktop.
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
//All letters to uppercase. Don't transform non-alphabetic characters (i.e. spaces, punctuation), but do pass them on.
function rot13(str) {
var codeArr = [], seq, final = "";
for (var i = 0; i < str.length; i++) {
uCode = str.charCodeAt([i]);
if ((uCode > 64 && uCode < 78) || (uCode > 96 && uCode < 110)) {
uCode += 13;
codeArr.push(uCode);
} else if ((uCode > 77 && uCode < 91) || (uCode > 109 && uCode < 123)){
uCode -= 13;
codeArr.push(uCode);
} else { codeArr.push(uCode); }
}
console.log(codeArr);
for (var j = 0; j < codeArr.length; j++) {
final += String.fromCharCode(codeArr[j]);
}
return final;
}
// Change the inputs below to test
rot13("SERR CVMMN!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment