Last active
February 1, 2022 12:28
-
-
Save furudean/32fdeb24ce2c349c5c53661bd5720b1c to your computer and use it in GitHub Desktop.
ROT13 JavaScript implementation
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
| function mod(a, n) { | |
| return ((a % n) + n) % n | |
| } | |
| export function rot(str, shift) { | |
| const a = "a".charCodeAt(0) | |
| const z = "z".charCodeAt(0) | |
| const A = "A".charCodeAt(0) | |
| const Z = "Z".charCodeAt(0) | |
| let msg = "" | |
| for (let i = 0; i < str.length; i++) { | |
| let code = str.charCodeAt(i) | |
| if (code >= a && code <= z) { | |
| code = mod(code - a + shift, 26) + a | |
| } | |
| if (code >= A && code <= Z) { | |
| code = mod(code - A + shift, 26) + A | |
| } | |
| msg += String.fromCharCode(code) | |
| } | |
| return msg | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment