Skip to content

Instantly share code, notes, and snippets.

@sedera-tax
Created March 21, 2016 08:27
Show Gist options
  • Save sedera-tax/c3bb71d4e7de2b5f200b to your computer and use it in GitHub Desktop.
Save sedera-tax/c3bb71d4e7de2b5f200b to your computer and use it in GitHub Desktop.
rot13
function rot13(str) { // LBH QVQ VG!
var res = [];
for(var i=0; i<str.length; i++){
if(str[i] !== " " && str[i] !== "!" && str[i] !== "?" && str[i] !== "." && str[i] !== ","){
var code = (str.charCodeAt(i) + 13);
if(code > 90){
code = code - 26;
}
//(c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
res.push(String.fromCharCode(code));
}
else{
if(str[i] === " "){
res.push(" ");
}
else if(str[i] === "!"){
res.push("!");
}
else if(str[i] === "?"){
res.push("?");
}
else if(str[i] === "."){
res.push(".");
}
else if(str[i] === ","){
res.push(",");
}
}
}
return res.join("");
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!")
rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK.");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment