Skip to content

Instantly share code, notes, and snippets.

@jgresalfi jgresalfi/shiftCipher.js
Last active Sep 25, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.