Created
November 2, 2012 08:00
-
-
Save qmacro/3999375 to your computer and use it in GitHub Desktop.
GoogleAppsScript ROT functions
This file contains 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
var alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
function rotText(text, n) { | |
var newText = ""; | |
for (var i = 0, j = text.length; i < j; i++) { | |
newText += rot(text[i], n); | |
} | |
return newText; | |
} | |
function rot(letter, n) { | |
var index = alphabet.indexOf(letter.toLowerCase()); | |
if (index < 0) return letter; | |
var isUpperCase = letter == letter.toUpperCase(); | |
var newIndex = (index + n) % alphabet.length; | |
var newLetter = alphabet[newIndex]; | |
return (isUpperCase) ? newLetter.toUpperCase() : newLetter; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See https://plus.google.com/103231502413952116096/posts/Sppj1TqUsVC for background.