Last active
April 7, 2022 10:18
-
-
Save proudlygeek/df29378cddaa71125c54 to your computer and use it in GitHub Desktop.
ROT13 Kata
This file contains hidden or 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
| // http://www.codewars.com/kata/52223df9e8f98c7aa7000062/train/javascript | |
| function rot13(str) { | |
| var lower = "abcdefghijklmnopqrstuvwxyz", | |
| upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", | |
| mod = upper.length; | |
| return str | |
| .split('') | |
| .map(function(i) { | |
| return i.match(/[a-zA-Z]/) | |
| ? (i.charCodeAt(0) < 90) | |
| ? upper[(upper.indexOf(i) + 13) % mod] | |
| : lower[(lower.indexOf(i) + 13) % mod] | |
| : i; | |
| }) | |
| .join(''); | |
| } |
This file contains hidden or 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
| Test.expect(rot13("EBG13 rknzcyr.") == "ROT13 example.") | |
| Test.expect(rot13("How can you tell an extrovert from an\nintrovert at NSA? Va gur ryringbef,\ngur rkgebireg ybbxf ng gur BGURE thl'f fubrf.") == "Ubj pna lbh gryy na rkgebireg sebz na\nvagebireg ng AFN? In the elevators,\nthe extrovert looks at the OTHER guy's shoes.") | |
| Test.expect(rot13("123") == "123") | |
| Test.expect(rot13("Guvf vf npghnyyl gur svefg xngn V rire znqr. Gunaxf sbe svavfuvat vg! :)") == "This is actually the first kata I ever made. Thanks for finishing it! :)") | |
| Test.expect(rot13("@[`{") == "@[`{") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment