Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. function caesarShift(str, amount) {
  4.    
  5.         var output = '';
  6.        
  7.         for (var i = 0; i < str.length; i ++) {
  8.    
  9.             // Getting current character
  10.             var current = str[i];
  11.  
  12.             // Checking if it is a letter.
  13.             if (current.match(/[a-z]/i)) {
  14.    
  15.                 // Get its ASCII value
  16.                 var asciiValue = str.charCodeAt(i);
  17.                
  18.                 // Encrypting uppercase letters
  19.                 if ((asciiValue >= 65) && (asciiValue <= 90))
  20.                     current = String.fromCharCode((((asciiValue - 65) + parseInt(amount)) % 26) + 65);
  21.    
  22.                 // Encrypting lowercase letters
  23.                 else if ((asciiValue >= 97) && (asciiValue <= 122)){
  24.                    current = String.fromCharCode((((asciiValue - 97) + parseInt(amount)) % 26) + 97);
  25.                    
  26.                 }
  27.             }
  28.    
  29.             // Appending everything
  30.             output += current;
  31.    
  32.         }
  33.             return output;
  34.    
  35.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement