Last active
August 2, 2018 16:47
-
-
Save egeozcan/4671503 to your computer and use it in GitHub Desktop.
Caesar cipher algorithm in javascript
This file contains 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
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