麻烦老师检查

来源:4-8 编程练习

最后的魔法使

2020-04-26 21:43:20

<!DOCTYPE html>

<html>

<head>

    <title> 事件</title>

</head>

<body>

    <input type='text' id='num1' />

    <select id='operate'>

        <option value='+'>+</option>

        <option value="-">-</option>

        <option value="*">*</option>

        <option value="/">/</option>

    </select>

    <input type='text' id='num2' />

    <input type='button' value=' = ' onclick="count()" />

    <input type='text' id='result' />

    <script type="text/javascript">

        function count() {

            /*此处写代码*/

            var inputs = document.querySelectorAll("input[type='text']"),

                values = [inputs[0].value,inputs[1].value],

                operate = document.querySelector('#operate');

            

            var cal = {

                add: function () {

                    return +(arguments[0]) + +(arguments[1]);

                },

                substract: function () {

                    return arguments[0] - arguments[1];

                },

                multiply: function () {

                    return arguments[0] * arguments[1];

                },

                divide: function () {

                    return arguments[0] / arguments[1];

                }

            };


            function output(params) {

                inputs[inputs.length-1].value = cal[params].apply(this, values);

            }


            switch (operate.value) {

                case '+':

                    output('add');

                    break;

                case '-':

                    output('substract');

                    break;

                case '*':

                    output('multiply');

                    break;

                case '/':

                    output('divide');

                    break;

            }


        }

    </script>

</body>

</html>



写回答

1回答

好帮手慕夭夭

2020-04-27

同学你好,代码实现正确,很棒! 继续加油,祝学习愉快 !

0

0 学习 · 14456 问题

查看课程