Ceaser's Cipher / Rot13 Challenge

I have the logic down and all the characters are translating nicely I am just struggling to figure out how to keep the punctuation of the original string in my new one. I’m sure this has something to do with the way I set up this problem to push into an array.

Any help would be greatly appreciated!


function rot13(str) {

const abc = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz".toUpperCase();

let arr = [];

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


  if(abc.includes(char,0)){
    let index = abc.indexOf(char,0) + 13;
  
  
  
    arr.push(abc[index]);
  }
}
let replace = arr.join('');
console.log(replace);


}
rot13("SERR CVMMN!");

Challenge: Caesars Cipher

Link to the challenge:

Continuing the discussion from Ceaser's Cipher / Rot13 Challenge:

I GOT IT!!!

function rot13(str) {
  const abc = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz".toUpperCase()

  let res = '';
  
  for(let i = 0; i < str.length; i++){
    let char = str[i];

    if(abc.includes(char,0)){
      let index = abc.indexOf(char,0) + 13;
     res += abc[index]
    }else{
     res += str[i]
    }
  }
return res;

  
}
rot13("SERR CVMMN!");
rot13("SERR PBQR PNZC");
rot13("SERR YBIR?");
rot13("GUR DHVPX OEBJA SBK WHZCF BIRE GUR YNML QBT.");

I edited your post to include spoiler tags since this is a working solution.

Please don’t take over somebody else’s thread. If you want feedback on your solution, please create a new topic. Thanks.

1 Like