/
spirzen
/
it-code-examples
Обзор
Документация
Войти
/
spirzen
/
it-code-examples
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
examples/javascript/js-501-103-014/main.js
31 строка
1 KB
Spirzen
Code migration pack
09 июн 2026, 01:41
09 июн 2026, 01:41
cebc284
Код
Авторство
О чём код?
// Этот код работает и в браузере, и в Node.js function countWords(text) { if (typeof text !== 'string') { throw new Error('Входные данные должны быть строкой'); } const words = text.trim().split(/\s+/); const filteredWords = words.filter(word => word.length > 0); return { total: filteredWords.length, unique: new Set(filteredWords).size, list: filteredWords }; } // Режим выполнения if (typeof window === 'undefined') { // Мы находимся в Node.js const textInput = "JavaScript - это язык программирования. JavaScript очень популярен."; const result = countWords(textInput); console.log('Текст:', textInput); console.log('Всего слов:', result.total); console.log('Уникальных слов:', result.unique); console.log('Список слов:', result.list); } else { // Мы находимся в браузере document.getElementById('result').innerText = JSON.stringify(countWords(document.getElementById('input').value)); }