Skip to content

Instantly share code, notes, and snippets.

@egeozcan
Last active August 2, 2018 16:47
Show Gist options
  • Save egeozcan/4671503 to your computer and use it in GitHub Desktop.
Save egeozcan/4671503 to your computer and use it in GitHub Desktop.
Caesar cipher algorithm in javascript
String.prototype.caesarCipher = function(level) {
function nextLetter(s){
return s.replace(/([a-zA-Z])[^a-zA-Z]*$/, function(a){
var c= a.charCodeAt(0);
switch(c){
case 90: return 'A';
case 122: return 'a';
default: return String.fromCharCode(++c);
}
});
}
var res = "", temp;
for(var i = 0; i < this.length; i++) {
temp = this[i];
for(var y = 0; y < level; y++) temp = nextLetter(temp);
res += temp;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment