Created
March 21, 2016 08:27
-
-
Save sedera-tax/c3bb71d4e7de2b5f200b to your computer and use it in GitHub Desktop.
rot13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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