Caesars Cipher - Help! Idk what my code is doing

Tell us what’s happening:
I’ve used a million console.log() and I still don’t quite understand what my code is doing. Clearly, I’ve done something wrong in one (or both) of the for loops, because it keeps adding the same letter to the string that’s supposed to be the output. Plus, the added letter is not even the one that should go for the input string’s first letter.
I really wouldn’t want to cheat and search for answers, even if my code is a little messy.

I’d appreciate any help, thanks in advance!

  **Your code so far**

function rot13(str) {

let abc = ['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'];
let rotabc = ['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 = '';
let regex = /[A-Z]+/;
let roted = str.split('');
for(let i = 0; i < roted.length; i++) {
if (regex.test(roted[i])) {
for (let x = 0; x < roted.length; x++) {
if (roted[x] === rotabc[x]) {
result += abc[x]
}
}
}
else {
result += roted[i];
};
};
return result;
}

rot13("SERR PBQR PNZC");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0

Challenge: Caesars Cipher

Link to the challenge:

function rot13(str) {
  let abc = ['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'];
  let rotabc = ['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 = '';
  let regex = /[A-Z]+/;
  let roted = str.split('');
  for (let i = 0; i < roted.length; i++) {
    if (regex.test(roted[i])) {
      for (let x = 0; x < roted.length; x++) {
        if (roted[x] === rotabc[x]) {
          result += abc[x]
        }
      }
    } else {
      result += roted[i];
    };
  };
  return result;
}

rot13("SERR PBQR PNZC");

I formatted your code so it is easier to read.

What exactly are you doing with the regex? I think you have a mixup of two different approaches and they are fighting each other.

Thanks for the answer!

I used the regex test so that the for loop with the comparison between the input string and the array with the ROT13, only runs when the item the iteration is at is a letter. Otherwise, it should add it to the string directly, as the spaces and other characters have to be added too (at least that was the intention of the regex test part haha)

Please excuse if I’m not expressing myself very well, english is not my first language.

Something isn’t arranged the way you intend. Using a regex to check for letters makes sense to me, but how it’s interacting with what’s inside of that is confusing.

The outermost loop is over all characters in your input string.

Then you have the if statement with the regex checking if a specific character is a letter.

Then you have another loop over all characters in the output string?

Then you have another if statement checking if the character in the output string matches the character in the same location in the rotated array?

Done!

Thank you SO much for lending me a hand without giving me the solution.
Reading your answer made me realize I was looping incorrectly in the second loop.
I fixed it by changing the second loop to iterate through the ROT13 array and comparing the iteration in the first loop with the one in the second loop. And if they were the same, adding the letter of the abc array that has the same position in the ROT13 array, to the output string.
I don’t know if I should paste the final code here, as I think there are no hints for the Projects, and maybe It wouldn’t be correct??

Again, thanks for taking the time to check my code!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.