Skip to content

Instantly share code, notes, and snippets.

@JamesCalleja
Created November 23, 2017 17:34
Show Gist options
  • Save JamesCalleja/f4d30ea6274254bbf8dd5abd5021cd16 to your computer and use it in GitHub Desktop.
Save JamesCalleja/f4d30ea6274254bbf8dd5abd5021cd16 to your computer and use it in GitHub Desktop.
/*
Wirten by James Calleja
32/11/2017
*/
function rot13(str)
{
var strArray = str.split("");
var result = [];
for (var i = 0; i < str.length; i++)
{
if (strArray[i].match(/[a-zA-Z]/)) //if isAlpha()
{
if (strArray[i].charCodeAt() -13 <= 64)
{
result.push(String.fromCharCode(strArray[i].charCodeAt() - 13 + 26));
}
else
{
result.push(String.fromCharCode(strArray[i].charCodeAt() - 13));
}
}
else
{
result.push(String.fromCharCode(strArray[i].charCodeAt()));
}
}
return result.toString().replace(/,/g,"");
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment