Hi, I’m working and stuck on one of the problems of front-end project. Here’s the explanation of the problem. "One of the simplest and most widely known ciphers is a Caesar cipher, also known as a shift cipher. In a shift cipher the meanings of the letters are shifted by some set amount.
A common modern use is the ROT13 cipher, where the values of the letters are shifted by 13 places. Thus ‘A’ ‘N’, ‘B’
‘O’ and so on.
Write a function which takes a ROT13 encoded string as input and returns a decoded string.
All letters will be uppercase. Do not transform any non-alphabetic character (i.e. spaces, punctuation), but do pass them on."
I wrote this code and it out puts proper deciphered English words but doesn’t out puts any signs correctly. White spaces are out put as red circles. Please teach me how to leave signs intact.
function rot13(str) { // LBH QVQ VG!
var words = str.split('');
for (var i=0;i<words.length;i++){
if (words[i].match(/[A-Z]/)) {
words[i] = words[i].charCodeAt();
}
if (words[i]>=78 && words[i]<=91){
words[i]-=13;
} else if (words[i]>=65 && words[i]<=78){
words[i]+=13;
}
}
for (var j=0; j<words.length; j++){
words[j] = String.fromCharCode(words[j]);
}
var result = words.join('');
return result;
}