Skip to content

Instantly share code, notes, and snippets.

@akiva
Last active October 8, 2015 07:00
Show Gist options
  • Save akiva/84368560b0875db5eae6 to your computer and use it in GitHub Desktop.
Save akiva/84368560b0875db5eae6 to your computer and use it in GitHub Desktop.
var caesar = {
encode: function encode(plainText, shift) {
if (!plainText) throw new Error('No plaintext provided');
shift = Number(shift) || 0;
return encryptText(plainText, shift);
}
};
function encryptText(str, shift) {
str = str.toUpperCase().replace(/[^\w\s]/gi, '');
var specialCodes = [32, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
var shiftedCharCodes = str.split('').map(function (s) {
var charCode = s.charCodeAt(0);
if (specialCodes.indexOf(charCode) !== -1) return charCode;
var newCharCode = charCode + shift;
if (newCharCode < 64) newCharCode += 26;
if (newCharCode > 90) newCharCode -= 26;
return newCharCode;
});
var cipher = String.fromCharCode.apply(null, shiftedCharCodes);
return cipher;
}
console.log(caesar.encode(process.argv[2], process.argv[3]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment