Caaesars cipher challenge

so i redid it using an array. so i corrected the spaces by putting a regex at the end. but i still cant figure out how to get the puncuation in the end back for the others to pass.

function rot13(str) {
  let arr=[];
  let temp='';
  
  let strValue=str.charAt(str.length-1)
  let newstr=str.split("")

  
  
  let testChar ='M';
  let testValue= testChar.charCodeAt()
  let result="";
  let regex=/[^A-Z a-z0-9]/;
 

for (let i = 0; i < newstr.length; i++){
          
      if(newstr[i].charCodeAt() > testValue  ){
        result= String.fromCharCode(newstr[i].charCodeAt()-13);
        arr.push(result);                                          
      }else if (newstr[i].charCodeAt() <= testValue  ){
          result= String.fromCharCode(newstr[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," ");
console.log(str)

return str;
  
}

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

So this gets back to me asking why change non-letters in the first place. Instead of putting a special case for spaces and punctuation after you’ve changed it, why not only change the letters?

when u iterate through the loop it just changes the non-letters. i dunno how to prevent the if test from doing it. been trying to do some hard codes around it but not working

i see what your getting at. if theres a space skip/ignore it . if i see a special character leave it alone. i’d love to figure out how to do that

unless looping isnt the best choice because it will evaluate the statement when it iterates.

You’re already checking if the character is before or after M… Why not check if the character is between A and Z as well?

oh i see so an "and " statement within the iff structure checking for less than or greater than m and and a regex test?

Sure, you could use regex or just use another couple of <= signs.

when i used regex , it got rid of the spaces and special character at the end now it outputs FREECODECAMP

It depends on how you are using the regex. Are you just removing the non-letters characters? You don’t want to do that.

i was just seeing if it made adifference. did [A-Z].test(newstr[i]) like you mentioned

im thinking abougt how to set up the <= statements like you mentioned

if(newstr[i].charCodeAt() > testValue && newstr[i].charCodeAt() <=testZ){
        result= String.fromCharCode(newstr[i].charCodeAt()-13 );
        arr.push(result);                                          
      }else if (newstr[i].charCodeAt() <=testValue && newstr[i].charCodeAt() >=testA ){
          result= String.fromCharCode(newstr[i].charCodeAt()+13);
         arr.push(result); 

this is the only change i made to my code and its still not ignoring the  special character and  and i still get no spacing.

And how are you defining testA and testZ

ooh for got about that ‘Z’.charCodeaT()
testA = ‘A’.charCodeaT()

my question is why did the special character just dissappear? , if the if structure tells the computer not to modify it it just stays unchanged as a special char and not get deleted no ?

Does your current code work?

Depending on the version that you are talking about, you were changing the non-letters in your previous code.

yes and no. got rid of the dashes between characters. but the punctuation at the end also dissappear.

function rot13(str) {
  let arr=[];
  let temp='';
  
  //let strValue=str.charAt(str.length-1)
  let newstr=str.split("")

 // let lastElement=newstr[newstr.length-1]
 

  let testA='A'.charCodeAt();
  let testZ='Z'.charCodeAt();
  let testChar ='M';
  let testValue= testChar.charCodeAt()
  let result="";
  //let regex=/[A-Z" "]/;
 

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

      }

}

     
arr=arr.join(""); 

console.log(arr)

return arr;
  
}

Ah, I see. If the character isn’t changed, it still needs to be pushed onto arr.

yeah thats the thing, it isnt getting pushed. even though the statement follows the iff test. is the limiter im using not iterating to the last element and skipping the last push?