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.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Here are some helpful links:
String.prototype.charCodeAt() String.fromCharCode()
function rot13(str) { // LBH QVQ VG!
//Создаем еррей с алфавитом,дописываем в него буквы, чтобы не париться насчет перевода Z+13, например
var alphabet =[ 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z', 'A','B','C','D','E','F','G','H','I','J','K','L','M'];
//Разбиваем стринг на буквы, чтобы сравнивать с alphabet
str = str.split('');
//Создаем пустой еррей для пуша дешифрованных букв
var decode = [];
for(var i = 0; i<str.length; i++){//итерируем str
if(alphabet.indexOf(str[i])===-1){//Если находим знаки вне alphabet(,?!б етс) - тупо пушим их в дешифровку
decode.push(str[i]);
}else{
for(var j = 0; j< alphabet.length; j++){//Итерируем буквы в алфавите
if(str[i]===alphabet[j]){//Если находим совпадение в зашифрованных буквах с алфавитом
decode.push(alphabet[j+13]);//Берем эту букву(ее индекс, номер) и добавляем к нему 13 -получаем дешифрованный индекс номер, пушим в дешифровку
}
}
}
}
return decode.join('');
}
// Change the inputs below to test
rot13("SERR PBQR PNZC");