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
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
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
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?