Skip to content

Instantly share code, notes, and snippets.

@qmacro
Created November 2, 2012 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qmacro/3999375 to your computer and use it in GitHub Desktop.
Save qmacro/3999375 to your computer and use it in GitHub Desktop.
GoogleAppsScript ROT functions
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;
}
@qmacro
Copy link
Author

qmacro commented Nov 2, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment