Javascript String Prototype Create a ROT13 Cypher with a Custom Function
function rot13(string) { return string.replace(/[a-z]/ig, function (character) { let moveNumber = character.toLowerCase() < 'n' ? 13 : -13; character = character.charCodeAt(0) + moveNumber; return String.fromCharCode(character); }); // w ww . ja va 2 s . c o m } console.log(rot13('hello')); console.log(rot13('HELLO')); console.log(rot13('uryyb'));
function rot13(str) { var newStr = ""; var intCode = NaN; for (var i = 0; i < str.length; i++) { if (str.charCodeAt(i) == " ") { // do nothing } else {// w w w .ja v a2 s .c om intCode = parseInt(str.charCodeAt(i)); if (intCode < 65 || intCode > 90) { newStr = newStr + String.fromCharCode(intCode); } else { intCode += 13; if (intCode > 90) { intCode = (intCode - 90) + 64; } newStr = newStr + String.fromCharCode(intCode); } } } return newStr; } console.log(rot13("!")); console.log(rot13("SERR PBQR PNZC")); console.log(rot13("SERR CVMMN!")); console.log(rot13("SERR YBIR?")); console.log(rot13("GUR DHVPX OEBJA QBT WHZCRQ BIRE GUR YNML SBK."));