Nodejs String Rot13 rot13(str)

Here you can find the source of rot13(str)

Method Source Code

// Caesars Cipher
// Write a function which takes a ROT13 encoded string as input and returns a decoded string.

function rot13(str) { // LBH QVQ VG!
    var arr = [];

    for (var i = 0; i < str.length; i++) {
        if (str.charCodeAt(i) < 65 || str.charCodeAt(i) > 90) {
            arr.push(str.charAt(i));//from w  ww .j a  va  2s  .c  o  m
        } else if (str.charCodeAt(i) > 77) {
            arr.push(String.fromCharCode(str.charCodeAt(i) - 13));
        } else {
            arr.push(String.fromCharCode(str.charCodeAt(i) + 13));
        }
    }
    return arr.join('');
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");


//Alternative solution provided by:
// thanks @anuragaryan @SaintPeter @vaskezu @abhisekp for your help with Algorithm: Caesar's Cipher

function rot13(str) { // LBH QVQ VG!
    return str.replace(/[A-Z]/g, (L) => String.fromCharCode(65 + (L.charCodeAt(0) - 65 + 13) % 26));
}

/*
Code Explanation:

String.prototype.replace function lets you transform a String based on some pattern match (defined by a regular expression),
and the transformation function (which is applied to each of the pattern matches).

Arrow function syntax is used to write the function parameter to replace().
L represents a single unit, from every pattern match with /[A-Z]/g - which is every uppercase letter in the alphabet,
from A to Z, present in the string.

The arrow function applies the rot13 transform on every uppercase letter from English alphabet present in the given string.
*/

Related

  1. rot13()
    var mail = "asdf@asdf.pbz"
    String.prototype.rot13 = function(){
      return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
      });
    };
    
  2. rot13()
    String.prototype.rot13 = function(){
        return this.replace(/[a-zA-Z]/g, function(c){
            return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
        });
    };
    
  3. rot13()
    String.prototype.rot13 = function () {
      return this.replace(/[a-zA-Z]/g, function (c) {
        return String.fromCharCode(((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26));
      });
    };
    var includeEmailLink = function () {
      var anchor = document.getElementById('hello');
      var href = anchor.getAttribute('href');
      return anchor.setAttribute('href', href.rot13());
    ...
    
  4. rot13()
    String.prototype.rot13 = function(){
      return this.replace(/[a-zA-Z]/g, function(c){
        return String.fromCharCode((c <= "Z" ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26);
      });
    };
    
  5. rot13(s)
    String.prototype.rot13 = function(s)
        return (s ? s : this).split('').map(function(_)
            if (!_.match(/[A-za-z]/)) return _;
            c = Math.floor(_.charCodeAt(0) / 97);
            k = (_.toLowerCase().charCodeAt(0) - 83) % 26 || 26;
            return String.fromCharCode(k + ((c == 0) ? 64 : 96));
         }).join('');
    ...
    
  6. rot13(str)
    function rot13(str) { 
      var codeZ ='Z'.charCodeAt(0);
      var codeA ='A'.charCodeAt(0);
      var arrStr =[];
      var strarr =[];
      var codeChar;
      console.log(codeZ);
      console.log(codeA);
      for (var i = 0; i < str.length; i++){
    ...