Caaesars cipher challenge

Tell us what’s happening:
Describe your issue in detail here.

below is my code to decode the caesars cipher project. i got them all decoded but i cant seem to figure out the puncuation at the end. that gets changed to.

  **Your code so far**

function rot13(str) {
let arr=[];
let newstr=str.split("");
let testChar ='M';
let test= testChar.charCodeAt()
let result="";
let regex=/[^a-zA-z0-9]/g;


for (let i = 0; i < newstr.length;i++){
      
      if(newstr[i].charCodeAt() > test){
           result= String.fromCharCode(newstr[i].charCodeAt()-13);
           arr.push(result);                                          
      }

      if(newstr[i].charCodeAt() <= test){
          result= String.fromCharCode(newstr[i].charCodeAt()+13);
          arr.push(result);
      }   
      if(newstr[i] === regex)
        newstr[i]=newstr[i];
      
}
arr=arr.join(" ");
str=arr.replace(/(-)/g,"");
console.log(str);


return str;
}

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


  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36

Challenge: Caesars Cipher

Link to the challenge:

What have you tried so far? What’s your guess what might work here?

i think i need to work on my regex expression

but i dunno why “Free Code Camp” isn;t passing

Have you confirmed the regex is doing anything right now? Like making it clearly wrong and seeing if anything changes in the results.

Take a look at the output in the console, there appears to be space between each character.

yeah just noticed that part. i guess i cant split the arrays up like that. and no the regex isnt affrcting anything

  let arr=[];
  let temp='';
  
  let strValue=str.charAt(str.length-1)
  
  let testChar ='M';
  let testValue= testChar.charCodeAt()
  let result="";
  let regex=/[^A-Z a-z0-9]/;
 

for (let i = 0; i <= str.length; i++){
          
      if(str.substring(i,i+1).charCodeAt() > testValue  ){
        result= String.fromCharCode(str[i].charCodeAt()-13);
        arr.push(result);                                          
      }else if (str.substring(i,i+1).charCodeAt() <= testValue  ){
          result= String.fromCharCode(str[i].charCodeAt()+13);
         arr.push(result); 
      }else if (strValue == regex.test(strValue)){
          str.replace((str.charAt(str.length-1)), strValue);

      }

}

      
arr=arr.join(""); 
str=arr.replace(/(-)/g," ");

return str  
  
}

rot13("SERR PBQR PNZC");
rot13("SERR CVMMN!")


heres my updated code. i got the first test to pass. but im trying to figure out what toi do with the punctuation. i tried saving the endstring in global and doing a  replace right before the return statement but that also didnt work

Could you explain how the replacing should work?

it would take the puncuation that got mutilated in the loop and replace it with the original character it started with if it is a non alphanumeric

But as you’ve written this, it is impossible to reach that case.

i tried doing another if at the end after the loop finished right before the return to replace the puncuation back but didnt work

Why not just not change it in the first place if it isn’t a letter?

You’re making your work harder by keeping this as a string. Why not switch to using an array?

i erased it though , i should of left the code as a comment

i started out as an array initially but it was doing funny things. separating each character with spaces and sometimes adding weird characters where theres supposed to be a space

when i was changing it in the first place in the loop, the it never went to the statement so i was trying different things

one thing when i had it as array format. i did str.split(" ") with the space . and i believe i think i had to do a nested forloop because it was separated into [“fullstring”, "nextstring] format.

You definitely should use .split("") instead of .split(" "). The second breaks apart your string on spaces. The first breaks apart your string character by character.

my question is when using .split(""). when i joined it into a string my output was “F R EE C O D E C A M P” those spaces made me fail. did to run it through a regex to remove the whitespace?

How did you join the characters? I’ll need to see more code; I can’t guess at what might have happened.

yeah i wished i saved it. ill redo the same code again . once im done ill show it to you