Великий Дешифровщик (массивы часть 1)

// Алфавит
var symbols = [‘А’, ‘Б’, ‘В’, ‘Г’, ‘Д’, ‘Е’, ‘Ё’, ‘Ж’, ‘З’, ‘И’, ‘Й’, ‘К’, ‘Л’, ‘М’, ‘Н’, ‘О’, ‘П’, ‘Р’, ‘С’, ‘Т’, ‘У’, ‘Ф’, ‘Х’, ‘Ц’, ‘Ч’, ‘Ш’, ‘Щ’, ‘Ъ’, ‘Ы’, ‘Ь’, ‘Э’, ‘Ю’, ‘Я’, ‘а’, ‘б’, ‘в’, ‘г’, ‘д’, ‘е’, ‘ё’, ‘ж’, ‘з’, ‘и’, ‘й’, ‘к’, ‘л’, ‘м’, ‘н’, ‘о’, ‘п’, ‘р’, ‘с’, ‘т’, ‘у’, ‘ф’, ‘х’, ‘ц’, ‘ч’, ‘ш’, ‘щ’, ‘ъ’, ‘ы’, ‘ь’, ‘э’, ‘ю’, ‘я’, ’ ', ‘.’, ‘,’, ‘—’, ‘!’];

// Закодированное сообщение
var encodedMessage = [18, 38, 46, 62, 66, 50, 33, 41, 66, 49, 48, 38, 58, 62, 68, 66, 48, 37, 42, 47, 66, 50, 33, 41, 66, 49, 48, 51, 49, 42, 67];

// Раскодированное сообщение
var decodedMessage = ‘’;

for(var i = 0; i<=encodedMessage.length-1; i++){
// console.log(encodedMessage[i]);

for(var k = 0; k <= symbols.length-1; k++){
if(encodedMessage[i]==k) {
decodedMessage += symbols[k];
}
}

}
console.log(decodedMessage);

Решение в одну строку в цикле для знакомых с модульной арифметикой

Моё решение

// Алфавит
let symbols = [‘А’, ‘Б’, ‘В’, ‘Г’, ‘Д’, ‘Е’, ‘Ё’, ‘Ж’, ‘З’, ‘И’, ‘Й’, ‘К’, ‘Л’, ‘М’, ‘Н’, ‘О’, ‘П’, ‘Р’, ‘С’, ‘Т’, ‘У’, ‘Ф’, ‘Х’, ‘Ц’, ‘Ч’, ‘Ш’, ‘Щ’, ‘Ъ’, ‘Ы’, ‘Ь’, ‘Э’, ‘Ю’, ‘Я’, ‘а’, ‘б’, ‘в’, ‘г’, ‘д’, ‘е’, ‘ё’, ‘ж’, ‘з’, ‘и’, ‘й’, ‘к’, ‘л’, ‘м’, ‘н’, ‘о’, ‘п’, ‘р’, ‘с’, ‘т’, ‘у’, ‘ф’, ‘х’, ‘ц’, ‘ч’, ‘ш’, ‘щ’, ‘ъ’, ‘ы’, ‘ь’, ‘э’, ‘ю’, ‘я’, ’ ', ‘.’, ‘,’, ‘—’, ‘!’];

// Смещение
let shift = 10;

// Закодированное сообщение
let encodedSymbols = [8, 28, 36, 52, 56, 40, 23, 31, 56, 39, 38, 28, 48, 52, 58, 56, 38, 27, 32, 37, 56, 40, 23, 31, 56, 39, 38, 41, 39, 32, 57];

// Раскодированное сообщение
let decodedMessage = ‘’;

for (let i = 0; i < encodedSymbols.length; i++) {
decodedMessage += symbols[(encodedSymbols[i] + shift) % symbols.length]
}

1 лайк