JavaScript Algorithms and Data Structures Projects: Caesars Cipher-help

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

1 Like

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

Thanks I’ll try that

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");

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

You are welcome.

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

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?

@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

function rot13(str) {
  var newStr = '',
      changeCode = 0;
  for (var i = 0; i < str.length; i++){
    if(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90){
      changeCode = str.charCodeAt(i) - 13
      if (changeCode < 65) {changeCode = str.charCodeAt(i) + 13};
      newStr += String.fromCharCode(changeCode);
    }else{
      newStr += str[i];
    }
  }
  return newStr;
}

OR

function rot13(str) {
  return str.replace(/[A-Z]/g, L =>
    String.fromCharCode((L.charCodeAt(0) % 26) + 65)
  );
}

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.
If you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

Also this topic has not been active for two years. Please try to comment on newer posts.

Thanks!