Caesars Cipher

Problem: Explained

As part of the Javascript Algorithms And Data Structures Certification you need to complete 5 javascript programs, Caesars Cipher being one of them. Up next I have quoted from freeCodeCamp's website the explanation for the DECODE button. The CODE button I have added, but it was not part of the exercise.

One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipherthe meanings of the letters are shifted by some set amount.

A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus 'A' ↔ 'N', 'B' ↔ 'O' and so on.

Write a function which takes a ROT13 encoded string as input and returns a decoded string.

All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on.

Solution: At Work


Solution:

Solution: The Code

function caesarsCipher(str) {
    let encoded = '';

    for (let i=0; i < str.length; i++) {
    let asciiNumber = str[i].charCodeAt();
        if (asciiNumber >= 65 && asciiNumber <= 77) {
            encoded += String.fromCharCode(asciiNumber + 13);
        } else if (asciiNumber >= 78 && asciiNumber <= 90) {
            encoded += String.fromCharCode(asciiNumber - 13); 
        } else {
            encoded += str[i];
        }
    }

    return encoded;
}

                                

The Solution: Step-by-Step

for (let i=0; i < str.length; i++)

This is a for loop that will run along, starting at 0, for as long as the variable i is less than the string's length (number of letter in this case).

let asciiNumber

You can have an array with all the letters of the alphabet, or you can use the ascii table. Ascii stands for American Standard Code for Information Interchange.

str[i].charCodeAt();

This piece of code will use the ascii table to convert the letter in the position i to its numerical equivalent.

Example: If the word is "GO" then str[0] is "G", with a charCodeAt() of "71". On the next loop, str[1] is "O", with a charCodeAt() of "79".

if (asciiNumber >= 65 && asciiNumber <= 77) {encoded += String.fromCharCode(asciiNumber + 13);}

The alphabet in the ascii table runs from 65 to 90. Per this exercise, in order to decode the message, each letter is shifted forward 13 positions.

Example: Using our previous example, the letter 'G' has a charcode of "71". If we add 71 + 13 we get 84. Using fromCharCode(84) we get the letter "T"

If we do the same with the letter "O" we get that 79 + 13 = 92. Using fromCharCode(92) we do not get a letter, but a '\'. this is the reason I have added the next part of the code.

else if (asciiNumber >= 78 && asciiNumber <= 90) {encoded += String.fromCharCode(asciiNumber - 13); }

With the numerical equivalent of the alpphabet running from 65 to 90, if we want to shift 13 positions forward from 90 we need to adjust the formula. Luckily this case is easy enoght, instead of adding we subtract.

Solving the problem we had with the letter "O", if instead of adding we do 79 - 13 = 66. Using fromCharCode(66) we get the letter "B"

else {encoded += str[i];}

What happens in the cases where we have spaces, numbers, lowercase or others? We just add them to the solution as they are. No changes.

return encoded;

The solution returned for the example used will be "TB".