How to Create a Caesars Cipher with JavaScript – Freecodecamp Project

How to Create a Caesars Cipher with JavaScript – Freecodecamp Project

The third project in Freecodecamp’s JavaScript Algorithms and Data Structures Certification tasks learners to write a Caesars Cipher program.

What is a Cipher?

A cipher refers to a secret or coded way of writing or transforming information to conceal its meaning. Caesars cipher is one type of cipher.

What is a Caesars cipher?

In a Caesars cipher, the meaning of the letters is shifted by a particular amount. Caesars cipher is also known as shift cipher.

The Caesars Cipher Challenge

This challenge requires the developer to write a function that takes a ROT13 encoded string as input and returns a decoded string. As stated in the challenge, the ROT13 cipher is a common modern use of Caesars cipher where the values of the letters are shifted by 13 places. Thus A = N, B = O, and so on.

How to Create a Caesars Cipher with JavaScript

One way to create a Caesars cipher requires the developer to do the following:

**1. Create an alphabet string. **This string of alphabets would be iterated on to return the correct ciphers.

let alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

2. Create an empty string to store the decoded message. I named mine cipherMessage

let cipherMessage = '';

3. Iterate through the string to check the index position of the characters. If the index is greater than or equal to position 13 of the alphabet, subtract 13 places from the index and return the letter positioned at the difference. For instance, the position of the letter S is greater than 13. Therefore, subtract 13 positions from it which gives F.

Here is the code to achieve this:

for (let i = 0; i < str.length; i++) {
    if (alphabet.indexOf(str[i]) >= 13) {
      cipherMessage += alphabet[alphabet.indexOf(str[i]) - 13]; }

4. But if the index is less than 13 and greater than -1 (0 -12), add 13 places to the index position and return the letter positioned at the sum. For instance, B is in position 2, which is less than 13 and greater than -1. Therefore, count 13 places from B which stops at O. Then return O.

Below is the code:

else if
        (alphabet.indexOf(str[i]) < 13 && alphabet.indexOf(str[i]) > -1) {
      cipherMessage += alphabet[alphabet.indexOf(str[i]) + 13];    }

5. Write an else statement to ensure that the spaces are included as required by the challenge.

else {
        cipherMessage += str[i];
      }

6. Then return cipherMessage

return cipherMessage;

That is it.

The entire solution will be:

function rot13(str) {
  let alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  let cipherMessage = '';
  for (let i = 0; i < str.length; i++) {
    if (alphabet.indexOf(str[i]) >= 13) {
      cipherMessage += alphabet[alphabet.indexOf(str[i]) - 13];    } else if
        (alphabet.indexOf(str[i]) < 13 && alphabet.indexOf(str[i]) > -1) {
      cipherMessage += alphabet[alphabet.indexOf(str[i]) + 13];    }
      else {
        cipherMessage += str[i];
      }
  }
  return cipherMessage;
}