Last active
January 31, 2016 06:24
-
-
Save jeffwesson/83e73444edc3e2308e4c to your computer and use it in GitHub Desktop.
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 reverseString(str) { | |
var chars = str.split(""), | |
letterNum = 1, | |
start = "A".charCodeAt(0), | |
end = "Z".charCodeAt(0), | |
list = []; | |
function opposite(char) { | |
var oppCharCode, | |
re = /[A-Za-z]/g; | |
if (!re.test(char)) { | |
return char; | |
} | |
oppCharCode = list.filter(function(obj) { | |
return obj.letter === char.toUpperCase(); | |
})[0].oppositeCharCode; | |
if (char.charCodeAt(0) > end) { | |
return String.fromCharCode(oppCharCode).toLowerCase(); | |
} else { | |
return String.fromCharCode(oppCharCode); | |
} | |
} | |
for (start; start <= end; start++) { | |
list.push({ | |
"alphabetNumber": letterNum, | |
"letter": String.fromCharCode(start), | |
"charCode": start, | |
"oppositeCharCode": (end + 1) - letterNum++ | |
}); | |
} | |
return chars.map(function(char) { | |
return opposite(char); | |
}).join(""); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment