Skip to content

Instantly share code, notes, and snippets.

@jialinhuang00
Last active October 2, 2017 09:34
Show Gist options
  • Save jialinhuang00/50527cb3ce71a376547b504614514256 to your computer and use it in GitHub Desktop.
Save jialinhuang00/50527cb3ce71a376547b504614514256 to your computer and use it in GitHub Desktop.
/*-----------------------------------------------------------------------------
1.turn every word into another word by the same process.
2.just like a puzzle
3.the map() method need to assigned to a variable.
the reference is by me, oh yah.
-----------------------------------------------------------------------------*/
function caesarCipher(str, num) {
arr = str.split("");
var ascII = arr.map(char => {
return char.charCodeAt();
});
console.log(ascII);
// ASCII: A-Z: 65-90 a-z:97-122
var transNum = ascII.map( (char) => {
// lowerCase
// upperCase, remember the equal sign
// if the char is not alphabet, then restore it.
if (char + num > 122) char -= 26;
else if (char + num > 90 && char >= 65 && char <= 90) char -= 26;
else if( char < 65) char -= num;
return (char += num);
});
var toOriginAlpha = transNum.map(char => {
return String.fromCharCode(char);
});
return toOriginAlpha.join("")
}
// zoo -> bqq
caesarCipher("Zoo Keeper", 26);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment