Skip to content

Instantly share code, notes, and snippets.

@andreasvirkus
Last active February 20, 2016 12:20
Show Gist options
  • Save andreasvirkus/ff156e613404c3062ce9 to your computer and use it in GitHub Desktop.
Save andreasvirkus/ff156e613404c3062ce9 to your computer and use it in GitHub Desktop.
function caesarShift(text, shift) {
var codes = [];
for (var i = 0; i < text.length; i++) {
var c = text.charCodeAt(i);
if (c >= 65 && c <= 90) {
c = (c - 65 + shift) % 26 + 65; // Uppercase
} else if (c >= 97 && c <= 122) {
c = (c - 97 + shift) % 26 + 97; // Lowercase
}
codes.push(c);
}
return String.fromCharCode.apply(String, codes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment