«

求最小值

yang 发布于 阅读:386 JS基础阶段


<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <input id="num1" type="text" />
        <input id="num2" type="text" />
        <input id="num3" type="text" />
        <button id="btn">求最小值</button>
        <input id="num4" type="text" />

        <script>
            // 1.获取对应的值
            // num1.value;
            // num2.value;
            // num3.value;
            // 2.给btn绑定点击事件onclick
            btn.onclick = function () {
                // 4.最小的值赋值给num4
                num4.value = getMin(num1.value, num2.value, num3.value);
            };

            // 一部曲 获取最小值 getMin
            // 二部曲 num1,num2,num3
            // 三部曲 return
            function getMin(num1, num2, num3) {
                // 3.点击时获取对应的值进行判断
                let a = Number(num1);
                let b = Number(num2);
                let c = Number(num3);
                if (a <= b && a <= c) {
                    return a;
                } else if (b <= a && b <= c) {
                    return b;
                } else if (c <= a && c <= b) {
                    return c;
                }
            }
        </script>
    </body>
</html>

求最小值

版权所有:微4e
文章标题:求最小值
除非注明,文章均为 微4e 原创,请勿用于任何商业用途,禁止转载

推荐阅读:


扫描二维码,在手机上阅读
请先 登录 再评论