Created
October 19, 2017 11:39
-
-
Save mmodrow/93b60e24fdb8511e80f23f96a4dcb54f to your computer and use it in GitHub Desktop.
Ceasar cypher in JS
This file contains hidden or 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
function caesar(char, offset){ | |
var charArray = Array.prototype.slice.call(char); | |
var output = ""; | |
charArray.forEach( function(singleChar){ | |
output += singleChar === " " ? " " : | |
String.fromCharCode(((((singleChar+"").toUpperCase().charCodeAt (0) + offset + 26) - 65)% 26)+ 65); | |
}); | |
return {message: output, offset: offset}; | |
} | |
// calling | |
// caesar(caesar ("Dies ist ein total toller Text", -3).message, 3) | |
// | |
// gives output: | |
// {message: "DIES IST EIN TOTAL TOLLER TEXT", offset: 3} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment