Skip to content

Instantly share code, notes, and snippets.

@carl-parrish
Created December 9, 2017 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carl-parrish/ec1977fe14eff69109427c0014e0e2cf to your computer and use it in GitHub Desktop.
Save carl-parrish/ec1977fe14eff69109427c0014e0e2cf to your computer and use it in GitHub Desktop.
Caesar Cipher
function caesarCipher(str, num) {
  [...arr] = str.toLowerCase();
  return arr.map((val,indx,input)=> {
    if (val.match(/[^a-z]/gi)) return val.charCodeAt(0);
    let code = input.join('').charCodeAt(indx) + num;
    return ( 96 < code && code < 123) ? code : 
    				(code >= 123) ? code - 26 : code + 26;
  });
  
}

let ans = String.fromCharCode(...caesarCipher('This is very important information',13));
console.log(ans);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment