const caesar = (str, offset) => Array
.from(str, n => String.fromCharCode(n.charCodeAt(0) + offset))
.join('');
const caesarTemplate = (title, offset) => `
<div class="caesar">
<textarea class="caesar-input" data-offset="${offset}"></textarea>
<hr>
<p>${title}: <span class="caesar-output"></span></p>
</div>
`;
document.addEventListener('input', ({ target: t }) => {
if (t.matches('.caesar-input')) {
const output = t.closest('.caesar').querySelector('.caesar-output');
output.innerHTML = caesar(t.value, +t.dataset.offset);
}
});
document.body.insertAdjacentHTML('beforeend', [
[ 'Шифр', 3 ],
[ 'Расшифровка', -3 ],
].map(n => caesarTemplate(...n)).join(''));