Project Feedback [Caesars Cipher]

Here is the problem, Caesars Cipher.

Here is my code:

function rot13(str) {
  function isLetter(str) {
    return str.match(/[A-Z]/);
  }
  let result = [];
  let garbage = {};
  let en = ['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 de = ['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'];

  for(let i=0; i<str.length; i++){
    for(let j=0; j<en.length; j++){
      if(str[i] === en[j]){
        result.push(de[j]);
      }
    }
  }
  for(let i=0; i<str.length; i++){
    if(!isLetter(str[i])){
      garbage[i] = str[i];
    }
  }
  let gKeys = Object.keys(garbage);
  for(let i =0; i<gKeys.length; i++){
    result.splice(parseInt(gKeys[i]),0,garbage[gKeys[i]])
  }
  return result.join("");
}

Do have a better idea, please let me know? :heart:

Hi again!

My comment is pretty similar to the last post. Based on these variable names, I am not sure what these variables are supposed to do. Choosing more descriptive variable names will help when people are looking at you code.
You want people to know exactly what those variables are supposed to do.

Hope that helps!

2 Likes

I like your regex data validation at the beginning. : )
I can understand that ‘en’ and ‘de’ probably stands for ‘encrypted’, ‘decrypted’, but as jwilkins said:

I have an idea, but will use a story, so you can think about it yourself.

Imagine that someone told you: “Ok, now do the same, but for lowercase letters”.
So you go and change every letter to lowercase.
Then that someone came back and told: “Hmm, all good, but I also want to change letter “A” to “P”, “B” to “Q” and so on. Hope that won’t be hard.”
So you go back to your code and make changes…
But you think to yourself “It is tedious to change that character list every time. How can I make it easier for myself?”

Good luck. : )

1 Like