JavaScript Algorithms and Data Structures Projects: Caesars Cipher-help

JavaScript Algorithms and Data Structures Projects: Caesars Cipher-help
0

#1

I need help with my Caesars Cipher project. I don’t know if my approach to this algorithm might work but all I’ve managed to do is basically create a alphabet and empty array for the characters that exists in the string, then I’m looping through the alphabet to see which characters exist in the str and pushing them to the empty array. That’s not what I want. I want to push the character that is 13 places away to the array. Do I need a totally different approach to this algorithm or can I fix my code so it can work and push the correct letter to the empty array?

function rot13(str) { // LBH QVQ VG!
  let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');
  let ceasar = [];

  alphabet.forEach(char => {
    if(str.indexOf(char) > -1) {
    ceasar.push(char);
    }
  });
  return ceasar;
}

// Change the inputs below to test
console.log(rot13("SERR PBQR PNZC"));

JavaScript Algorithms and Data Structures Projects: Caesars Cipher-help


#2

that might work somehow, but you may want to use the ASCII char code of characters , get a character’s char code, subtract 13 (and if get below the code of A subtract from the top), and convert the new char code to a character again


#3

Thanks I’ll try that


#4

Hi,

You can work like this:

function rot13(str) { // LBH QVQ VG!
  const input     = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  const output    = 'NOPQRSTUVWXYZABCDEFGHIJKLM';
  const index       = x => input.indexOf(x);
  const translate   = x => index(x) > -1 ? output[index(x)] : x;
  return str.split('').map(translate).join('');
}

// Change the inputs below to test
rot13("SERR PBQR PNZC");

#5

Thank you I thought of something like that just couldn’t put it together .


#6

You are welcome.

If you have any question about this structure do not hesitate to ask.


#7

thanks , can you just explain whats happening from const translate and return. How I understand it is if the index(x) (which is the alphabet in the input) exist, return the output at the current index(x) else return x. and then split each character in string into a array, map through and check if the condition is true, join the characters back into a string and return it… Is that correct?


#8

@CodeJuan88, yes that is true.

Below a solution easier to understand

function rot13(str) { // LBH QVQ VG!
  
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  const cipher = 'NOPQRSTUVWXYZABCDEFGHIJKLM'
  let newStr = ''

  for (const letter of str) {
    alphabet.charAt(cipher.indexOf(letter)) != '' ? 
    newStr += alphabet.charAt(cipher.indexOf(letter)) : newStr += letter
  }
  return newStr
}

the for...of is used to iterate through the argument str
the charAt() function returns an empty string if the character is not found, so I used a ternary conditional. If the character is found, the chiper string is used to concatenate newStr. Otherwise, it just concatenates the original character.

Hope it helps