Trying to get a JS function to work that shifts the individual characters in a string by a set amount (and then returns the new "shifted" string). I'm using ROT-13 (so A-M are shifted down 13 characters and N-Z are shifted up 13 characters).
The trouble lies with this piece of code:
if (arr[i] <= 77) {
finalStr += String.fromCharCode(arr[i] + 13);
This code should shift E to R.
E (69) + 13 = R (82)
However, the characters in the returned string that should be shifted down 13 spaces return as weird symbols.
"FᬁEEC᧕DECAMᨹ"
function rot13(str) {
var newStr = "";
var finalStr = "";
for (i = 0, j = str.length; i < j; i++) {
newStr += str.charCodeAt(i);
newStr += " ";
}
var arr = newStr.split(" ");
arr.pop();
for (i = 0, j = arr.length; i < j; i++) {
if (arr[i] !== 32) {
if (arr[i] <= 77) {
finalStr += String.fromCharCode(arr[i] + 13);
}
else {
finalStr += String.fromCharCode(arr[i] - 13);
}
}
}
return finalStr;
}
rot13("SERR PBQR PNZC");