Skip to content

Instantly share code, notes, and snippets.

@hhenrichsen
Created January 23, 2019 03:06
Show Gist options
  • Save hhenrichsen/2b939cb42e53a80e988281a4f257e09c to your computer and use it in GitHub Desktop.
Save hhenrichsen/2b939cb42e53a80e988281a4f257e09c to your computer and use it in GitHub Desktop.
const outerOffset = 65; //ASCII code of first letter
const innerOffset = outerOffset - 13; //Number that is subtracted to determine new ASCII code
const allowedChars = /[A-Z]/; //Characters to be transformed
var rot13 = (str) => str.toUpperCase() //Function declaration, accept lowercase
.split("") //Basic stream transforms
.map(c => (c.match(allowedChars) ? //Check if its a character to be transformed;
String.fromCharCode(outerOffset + ((c.charCodeAt(0) - innerOffset) % 26)) //Transform it
: c)) //Don't transform it
.join(""); //Join it back into a string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment