Показать сообщение отдельно
  #6 (permalink)  
Старый 06.06.2022, 21:31
Аватар для рони
Профессор
Отправить личное сообщение для рони Посмотреть профиль Найти все сообщения от рони
 
Регистрация: 27.05.2010
Сообщений: 33,070

дело было вечером...
<!DOCTYPE html>
<html>

<head> </head>

<body>
    <input id="input" />
    <button id="btn">Answer</button>
    <div id="console"></div>
    <script>
        class JsConsole {
            constructor(selector) {
                this.consoleElement = document.querySelector(selector);
                this.consoleElement.classList.add('js-console');
                this.textArea = document.createElement('p');
                this.consoleElement.appendChild(this.textArea);
            }

            write(text) {
                const textLine = document.createElement('span');
                textLine.innerHTML = text;
                this.textArea.appendChild(textLine);
                this.consoleElement.scrollTop = this.consoleElement.scrollHeight;
            }

            writeLine(text) {
                this.write(text);
                this.textArea.appendChild(document.createElement('br'));
            }

            read(inputSelector) {
                const element = document.querySelector(inputSelector);
                if (element.innerHTML) {
                    return element.innerHTML;
                } else {
                    return element.value;
                }
            }

            readInteger(inputSelector) {
                const text = this.read(inputSelector);
                return parseInt(text);
            }

            readFloat(inputSelector) {
                const text = this.read(inputSelector);
                return parseFloat(text);
            }
        }

        jsConsole = new JsConsole('#console');

        var word = [
            'ноль',
            'один',
            'два',
            'три',
            'четыре',
            'пять',
            'шесть',
            'семь',
            'восемь',
            'девять',
        ];

        function lastdigit() {
            const num = jsConsole.readInteger('#input');
            if (isNaN(num)) {
                jsConsole.writeLine('not number');
                return;
            }
            const n = num.toString().split('')[0];
            jsConsole.writeLine(word[n ?? 0]);
        }

        document.querySelector('#btn').onclick = lastdigit;
    </script>
</body>

</html>
Ответить с цитированием