Skip to content

Instantly share code, notes, and snippets.

@ian128K
Created October 2, 2014 05:24
Show Gist options
  • Save ian128K/d7edc90de235bfed0827 to your computer and use it in GitHub Desktop.
Save ian128K/d7edc90de235bfed0827 to your computer and use it in GitHub Desktop.
Function for shifting all letters in a string one letter forward in the alphabet
function letterShifter(message) {
var lc_alphabet, uc_alphabet, lc_key, uc_key, coded, i, ch, index;
lc_alphabet = "abcdefghijklmnopqrstuvwxyz";
uc_alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
lc_key = "bcdefghijklmnopqrstuvwxyza";
uc_key = "BCDEFGHIJKLMNOPQRSTUVWXYZA";
coded = "";
for(i = 0; i < message.length; i++) {
ch = message.charAt(i);
if(isNaN(ch * 1) === false) {
coded = coded + ch;
} else if(ch.match(/^[A-Za-z]+$/) === null) {
coded = coded + ch;
} else if(ch === ch.toLowerCase()) {
index = lc_alphabet.indexOf(ch);
coded = coded + lc_key.charAt(index);
} else if(ch === ch.toUpperCase()) {
index = uc_alphabet.indexOf(ch);
coded = coded + uc_key.charAt(index);
}
}
return coded;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment