Skip to content

Instantly share code, notes, and snippets.

@mmodrow
Created October 19, 2017 11:39
Show Gist options
  • Save mmodrow/93b60e24fdb8511e80f23f96a4dcb54f to your computer and use it in GitHub Desktop.
Save mmodrow/93b60e24fdb8511e80f23f96a4dcb54f to your computer and use it in GitHub Desktop.
Ceasar cypher in JS
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