Caesars Cipher solutions

Here is my solution :slight_smile:

function rot13(str) {
  // Define return variable
  let ans = "";

  // Get length of string
  let length = str.length;

  // Loop through each of the characters in the string
  for (let i = 0; i < length; i++) {
    // If character is a letter, apply cipher
    if (/[A-Z]/.test(str[i])) {
      ans += cipher(str[i]);
    }
    // If character is not a letter, retain
    else {
      ans += str[i];
    }
  }

  // Return encoded string
  return ans;
}

// Define function that applies the cipher to a character
const cipher = (char) => {
    // Convert character to ASCII and add 13
    let ascii = char.charCodeAt() + 13;
    // If ASCII exceeds 'Z', go back to 'A'
    if (ascii > 90) {
        ascii = 65 + (ascii - 90) - 1;
    }
    // Convert ASCII back to character and return it
    return String.fromCharCode(ascii);
}

you are welcome :smiley: :smiley:
yeah I like the idea of using %
still wonder why it doesn’t work while we were live streaming

Hey guys. Find my simple solution here:

function rot13(str) {

  let alphabets = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','A','B','C','D','E','F','G','H','I','J','K','L','M'];
  let result = '';
  for (let i = 0; i < str.length; i++){
    if (str[i] === '?'| str[i] ==='.'| str[i] ==='!'| str[i] === ' '){
        result = result.concat('', str[i]);  
          } 
    for(let j = 0; j < 26; j++){ 
          if(str[i] === alphabets[j]){
      result = result.concat((alphabets[(13 + alphabets.indexOf(alphabets[j]))]));
      
      }   
    }
  }
  return result;
  
  
}

console.log(rot13("SERR PBQR PNZC"));
console.log(rot13("SERR YBIR?"));
console.log(rot13("SERR CVMMN!"));

interesting solution Omar, I’m in awe!

This solution is the closest to everyday English I have seen for this particular algorithm. I’ll copy it, thanks.

Hello guys,

Here’s my not so elegant solution :joy:

(almost no brain cells were harmed in the making of this solution)

Solution #1

const CIPHER = {
     A:'N', B:'O', C:'P', D:'Q', E:'R', F:'S', G:'T', H:'U', I: 'V',
     J:'W', K:'X', L:'Y', M:'Z', N:'A', O:'B', P:'C', Q:'D', R:'E',
     S:'F', T:'G', U:'H', V:'I', W:'J', X:'K', Y:'L', Z:'M'
  };

function rot13(str) {
  let decoded = "";

  for(let i = 0; i < str.length; i++){
    if(CIPHER.hasOwnProperty(str[i]) === true){
      decoded += (CIPHER[str[i]]);
    }
    else if(CIPHER.hasOwnProperty(str[i]) === false){
      decoded += (str[i]);
    }
  }
  return decoded;
}
console.log(rot13("SERR. PBQR. PNZC?!"));

(Edit: Brushed up my original solution a bit after watching the video :laughing:)

After watching, I realized that my solution is not dynamic/flexible as it only works exclusively for ROT13. So I tried doing the project again. This time I can set the amount of shifting or rotation from 1 to 13.

(RIP brain cells)

Solution #2

const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // alphabet - 26 letters

function rot13(str){
  let decoded = ""; // storage for decoded string
  let newLetter = ""; // storage for the new/decoded letter based from its new index
  let newIndex; // storage for the new index based on the number of shifts
  let shift = 13; // the number of shift (applicable only from 1-13) 
  let maxShift = shift*2; // maximum index value a new/decoded letter can have

  for(let i = 0; i < str.length; i++){ // for loop to access every letter in the given string
    if(ALPHA.includes(str[i]) === true){ // if a letter in the given string is present at ALPHA (line 2)
        newIndex = ALPHA.indexOf(str[i]) + shift; // then the letter should move *shift* places
      if(newIndex < maxShift){ // if the new index of the new letter (line 13) is < maxShift (line 9)
        newLetter = ALPHA[newIndex]; // then store the new letter in the variable newLetter
        decoded += newLetter; // and then store newLetter to decoded variable
      }
      else if(newIndex >= maxShift){ // but if new index of the new letter (line 13) is >= maxShift (line 9)
        newIndex -= maxShift; // then subtract the new index of the new letter by maxShift (line 9)
        newLetter = ALPHA[newIndex]; // and then store the new/decoded letter based on its new index in the variable newLetter
        decoded += newLetter; // and then store newLetter to decoded variable
      }
    }
    else if(ALPHA.includes(str[i]) === false){ // if a letter in the given string is not present at ALPHA (line 2)
      decoded += str[i]; // then just store it to decoded variable 
    }
  }
  return decoded; // after looping, return the value of decoded variable out of the function 
};
console.log(rot13("SERR. PBQR. PNZC?!"));

Since I have the luxury of time and wanted to be extra, I tried to enable the number of rotations from 14 to 26 too like the one on this website

I realized after doing this I could have used charCodeAt() :woman_facepalming: But whatever. I like beating myself up with math problems anyway :joy:

(yep, I just love math)

Solution #3

const ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function rot13(str){
  let upperStr = str.toUpperCase();
  let decoded = ""; 
  let newLetter = ""; 
  let newIndex; 
  let rot = 13; 
  let maxIndex = 25 - rot; 

  for(let i = 0; i < upperStr.length; i++){
    if(ALPHA.includes(upperStr[i]) === true){
      if(ALPHA.indexOf(upperStr[i]) <= maxIndex){
        newIndex = ALPHA.indexOf(upperStr[i]) + rot;
        newLetter = ALPHA[newIndex];
        decoded += newLetter;
      }
      else if(ALPHA.indexOf(upperStr[i]) > maxIndex){
        newIndex = ALPHA.indexOf(upperStr[i]) - maxIndex - 1;
        newLetter = ALPHA[newIndex];
        decoded += newLetter;
      }
    }
    else if(ALPHA.includes(upperStr[i]) === false){ 
      decoded += upperStr[i];
    }
  }  
  return decoded;
};
console.log(rot13("abcdefghijklmnopqrstuvwxyz !?"))
1 Like