Created
October 2, 2014 05:24
-
-
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
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
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