web-tech/tasks/math-funcs/funcs/cesar.js
2025-11-23 14:00:44 +03:00

40 lines
No EOL
1.2 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function cesar(str, shift, action) {
const alphabet = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюя';
const alphabetUpper = 'АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ';
let result = '';
for (let i = 0; i < str.length; i++) {
let char = str[i];
let isUpper = false;
let index = -1;
if (alphabetUpper.indexOf(char) !== -1) {
isUpper = true;
index = alphabetUpper.indexOf(char);
} else if (alphabet.indexOf(char) !== -1) {
index = alphabet.indexOf(char);
}
if (index !== -1) {
let newIndex;
if (action === 'encode') {
newIndex = (index + shift) % alphabet.length;
} else {
newIndex = (index - shift + alphabet.length) % alphabet.length;
}
if (isUpper) {
result += alphabetUpper[newIndex];
} else {
result += alphabet[newIndex];
}
} else {
result += char;
}
}
return result;
}
// Расшифровка сообщения "эзтыхз фзъзъз" с различными сдвигами:
// При shift = 8: "хакуна матата"