[SOLVED]Caesars Cipher: Replaces "wrong" character

[SOLVED]Caesars Cipher: Replaces "wrong" character
0

#1

Tell us what’s happening:
Hello Dear Camper,
i already saw other solutions with CharToCode and i think i can do it with that approach. I would still like to know why the code below is behaving weird. For the example below i get from “SERR PBQR PNZC” the result “FEEE PODR CAMC”.
The third iteration might be interesting. It should change the str[2] to E, but it actually replaces str[1] with the desired character…

Thanks in advance for your help!

Your code so far


function rot13(str) { // LBH QVQ VG!
//Initialize Alphabet + 13 more characters for the shift
  let alphabet = ["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 regEx = /^[A-Z]+$/i;

  console.log(str);

  //ForLoop for every Character in str

  for(let i = 0; i < str.length; i++){

    //RegEx if Character is in Alphabet

    if(regEx.test(str[i])){

      //Find Character Index in Alphabet

      for(let x = 0; x < 26;x++){

        if(str[i]==alphabet[x]){

          //Replace Character with value of alphabet[index+13]

          str = str.replace(str[i], alphabet[x+13]);

          console.log(str);

          break;

        }

      }

    }

  }

  console.log(str);

  return str;

}

// Change the inputs below to test

rot13("SERR PBQR PNZC");

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/caesars-cipher


#2
str = str.replace(str[i], alphabet[x+13]);

The above line will replace the first instance of the value of str[i] in str with alphabet[x+13] .

Replace your entire outer for loop with a modified version of your original (see below) where I have added console.log statements so you can see what is happening with your replace.

  for(let i = 0; i < str.length; i++){

    //RegEx if Character is in Alphabet

    if(regEx.test(str[i])){

      //Find Character Index in Alphabet

      for(let x = 0; x < 26;x++){
        if(str[i]==alphabet[x]){

          //Replace Character with value of alphabet[index+13]
          console.log('str before replace => ' + str);
          console.log('replace first instance of ' + str[i] + ' in str with ' + alphabet[x+13])
          str = str.replace(str[i], alphabet[x+13]);
          console.log('str after replace => ' + str);  
          break;
        }
      }
      console.log()
    }
  }

See if this provides some insight of what is going on.


#3

Hi randelldawson, thanks for your quick reply.

Why is it exchanging the first instance, when i explicitely defined, that the position str[i] should be exchanged? I am struggeling to understand…


#4

str[i] is just a string (in this case a letter or a space or maybe a punctuation mark).

It would be the same as writing:

var char = str[i];
str = str.replace(char, alphabet[x+13]);

#5

Thanks a lot, that is enlightening!