Hi everyone)
While doing Caesars Cipher noticed one thing:
if (str[i].match(/[A-Z]/))
works not the same as:
if (str.charAt(i) == /[A-Z]/)
I’m not sure wether it is because of regular expression or maybe str(i) and str.charAt(i) returns different value.
I will be grateful for any response!
Complete code is here ( I commented line that doesn’t work)
function rot13(str) { // LBH QVQ VG!
var arr=[]; var num = 0; var letter = "";
for (var i = 0; i<str.length; i++) {
if (str[i].match(/[A-Z]/)) {
//if (str.charAt(i) === /[A-Z]/) {
num = str.charCodeAt(i)-13;
if (num < 65) { num += 26;}
letter = String.fromCodePoint(num);
}
else {
letter = str.charAt(i);
}
arr.push(letter);
}
str = arr.join("");
return str;
}
// Change the inputs below to test
rot13("SERR CVMMN!");