Skip to content

Instantly share code, notes, and snippets.

@furudean
Last active February 1, 2022 12:28
Show Gist options
  • Select an option

  • Save furudean/32fdeb24ce2c349c5c53661bd5720b1c to your computer and use it in GitHub Desktop.

Select an option

Save furudean/32fdeb24ce2c349c5c53661bd5720b1c to your computer and use it in GitHub Desktop.
ROT13 JavaScript implementation
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