Need help with Caesar's Cipher project

I tried a really different approach, and it almost works, but I can’t work out why only half of the cipher translates; I can’t see why the rotation issue would apply here.
ie. console.log(arr); // FEEE CBDE CAMC

:confused:

function rot13(str) {

 var arr = str.split("");
const alphaKey = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
const rot13Key = "NOPQRSTUVWXYZABCDEFGHIJKLM".split("");
  
for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < rot13Key.length; j++) {
      if (arr[i] === alphaKey[j]) {
        arr[i] = rot13Key[j];
      }
    }
  }
console.log(arr.join(""));
}

rot13("SERR PBQR PNZC");

Hi @hjm!

Welcome to the forum!

I moved your question out of the year old post and into a new topic because you will get more responses this way.

thank you so much! I really appreciate the help! My first time posting…

1 Like

let’s say that arr[i] is equal to A
so it is changed to N
the j loop continues and at one point arr[i] === alphaKey[j] is true again

can you see the issue?
that is also the reason of why exactly half the letters are not changed

2 Likes

Ah! is it that when later on, arr[i] is === N, because of the previous substitution, it can’t change back to A?

Thanks you so much, by the way. It’s been doing my nut in…

the issue is that it is changed back to A

1 Like

Thanks - I think I get it. I really appreciate you taking the time to help!

1 Like