Урок 5

<-- Start of s5.5\3.11.2.html --> 
<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Среднее арифметическое</title>
    </head>
    <body>
        <h1>Среднее арифметическое</h1>
        <script type="text/javascript">
            function average() {
                const cntAll = arguments.length;
                let sum = 0, cnt = 0, i, el;
                for (i = 0; i < cntAll; i++) {
                    el = arguments[i];
                    if (typeof el == 'number') {
                        sum += el;
                        cnt++;
                    }
                }
                if (cnt == 0) {
                    const ex = new TypeError();
                    ex.message = 'Функции average не был передан ни ' +
                        'один числовой параметр';
                    throw ex;
                }
                return sum / cnt;
            }
            try {
                const av = average(10, 20, 30, 40, 50, 60, 70, 80, 90, 100);
                window.document.write('<p>', av, '</p>');
            }
            catch(ex) {
                window.document.write('<p>', ex.message, '</p>');
            }
            try {
                const av = average('10', '20', '30', '40', '50', '60', '70',
                    '80', '90', '100');
                window.document.write('<p>', av, '</p>');
            }
            catch(ex) {
                window.document.write('<p>', ex.message, '</p>');
            }
            try {
                const av = average();
                window.document.write('<p>', av, '</p>');
            }
            catch(ex) {
                window.document.write('<p>', ex.message, '</p>');
            }
        </script>
    </body>
</html>
 
<-- End of s5.5\3.11.2.html --> 
 
<-- Start of s5.5\4.14.1.html --> 
<!doctype html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Среднее арифметическое</title>
    </head>
    <body>
        <h1>Среднее арифметическое</h1>
        <script type="text/javascript">
            const s = window.prompt('Введите список чисел', '');
            try {
                const cs = s.split(',');
                let sum = 0;
                cs.forEach((el) => {sum += +el;});
                const avg = sum / cs.length;
                window.document.write('<p>', avg, '</p>');
            }
            catch(ex) {
                window.document.write('<p>Список чисел не был введен</p>');
            }
        </script>
    </body>
</html>
 
<-- End of s5.5\4.14.1.html --> 
 

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Среднее арифметическое</title>
</head>
<body>
    <h1>Среднее арифметическое</h1>
    <script type="text/javascript">
        try {
            const s = window.prompt('Введите список чисел', '');
            if (s === null) {
                throw new Error('Ввод отменён пользователем');
            }

            const cs = s.split(',');
            let sum = 0;
            cs.forEach((el) => {
                sum += +el;
            });
            const avg = sum / cs.length;
            window.document.write('<p>Среднее: ', avg, '</p>');
        } catch (ex) {
            window.document.write('<p><strong>Ошибка:</strong> ', ex.message, '</p>');
        }
    </script>
</body>
</html>


<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Среднее арифметическое</title>
</head>
<body>
    <h1>Среднее арифметическое</h1>
    <script type="text/javascript">
        function average() {
            const cntAll = arguments.length;
            let sum = 0, cnt = 0;
            for (let i = 0; i < cntAll; i++) {
                const el = arguments[i];
                if (typeof el === 'number') {
                    sum += el;
                    cnt++;
                }
            }
            if (cnt === 0) {
                const ex = new TypeError('Функции average не был передан ни один числовой параметр');
                throw ex;
            }
            return sum / cnt;
        }

        const testCases = [
            [10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
            ['10', '20', '30', '40', '50', '60', '70', '80', '90', '100'],
            []
        ];

        testCases.forEach((args) => {
            try {
                const result = average.apply(null, args);
                window.document.write('<p>Результат: ', result, '</p>');
            } catch (ex) {
                window.document.write('<p><strong>Ошибка:</strong> ', ex.message, '</p>');
            }
        });
    </script>
</body>
</html>