According to learncryptography.com, the Caesar Cipher (aka a shift cipher), is a very simple form of encryption “where each letter in the original message is replaced with a letter corresponding to a certain number of letters up or down in the alphabet.”

In this solution, I step through my approach to constructing a Javascript function that emulates the Caesar Cipher. The function takes two arguments: the string to encrypt, and the shift constant. The shift constant determines how many letters we go up (if positive) or down (if negative) in the alphabet to replace each letter. For example, `caesarCipher("hello", 5)` should return `"mjqqt"`.

#### Outlining & Pseudocode

To start, let’s construct the shell of the function and get a sense for how it should work in the end.

#### Shifting a Letter

We’re going to use the ASCII code to shift the letters. We’ll use the `.charCodeAt()` method in order to get the ASCII code of each letter. Since all the letters of the alphabet are in order of ascending ASCII codes, all we have to do is add `shiftNum` to the character code of the current letter to get the new letter’s code. Then, we can use `.fromCharCode()` to convert back to the letter version.

In code,

Note that the callback function `shift` operates on one letter at a time, so we want the character code of the only letter in a one-letter string, which is at index 0, hence `letter.charCodeAt(0)`.

#### But Wait, There’s More.

What if we want to shift `"zebra"` by 3 characters? The way our function is set up right now, we’d end up with `"}jgwf"` because the character code for `122 + 3` corresponds to the symbol `"}"`. Likewise, if applying the shift constant results in a character code below 97, we’ll end up with a capital letter rather than the correctly shifted one.

Let’s do some math. If the resulting shifted code is greater than 122, we want to take the difference between our sum and 122, and add that to 97. Likewise, if the resulting shifted code is less than 97, we want to take the difference between our sum and 97, and subtract that number from 122. Translation:

Let’s drop that into our function.

#### Refactor & Get Creative

I think it makes more sense syntactically to append this method to the String prototype, so it can be used like “`"hello".encryptWithCaesars(3)` where `"hello"` is the string to be encrypted and the argument passed to the method is the shift constant.

Let me know what you think! Contact info in the footer - tweet at me with suggestions/comments. Hope I was able to pass on a little knowledge and help you out with this problem!