JS Math对象

209 阅读2分钟

Math对象是一个数学对象,不是一个构造函数。构造函数实例化对象是使用new的方式

1.1 利用Math对象取最大值和最小值

Infinity:正无穷
-Infinity:负无穷大
NaN:Not a Number
当float或double类型的数除零时,
当被除数为非零值时,结果为无穷大
当被除数也为零值时,则结果为NaN

    <script>
        console.log(Math.PI); // 3.1415926...
        // max
        console.log(Math.max(1, 100, 20));//100
        console.log(Math.max(-1, -100));//-1
        console.log(Math.max(1, 100, "你好"));//NaN
        console.log(Math.max());//-Infinity
        // min最小值
        console.log(Math.min(10, 20, 1));//1
    </script>

1.2 自己封装一个Math对象

<!--利用对象的方式来封装一个Math对象-->
<script>
        var myMath = {
            PI: 3.141592653,
            max: function() {
                var max = arguments[0];
                for(var i = 1; i < arguments.length; i++) {
                    if(arguments[i] > max) {
                        max = arguments[i]
                    }
                }
                return max;
            },
            min: function () {
                var min = arguments[0];
                for (var i = 1; i < arguments.length; i++) {
                    if (arguments[i] < min) {
                        min = arguments[i]
                    }
                }
                return min;
            }
        }

        console.log(myMath.PI)
        console.log(myMath.max(1, 2, 3, 10))
        console.log(myMath.min(1, 2, 3, 10))
    </script>

1.3 利用Math求绝对值,向上向下取整等

<script>
        // 1.绝对值!!!
        //var a = -1;
        console.log(Math.abs(1))
        console.log(Math.abs(-1))
        console.log(Math.abs('red'))//NaN
        // 隐式转换的过程
        console.log(Math.abs("-1"))
        // 2.取整  
        // 2.1 向下取整数
        var a = 2.1;
        console.log(Math.floor(a)) // 2
        // 2.2 向上取整
        var a = 3.1
        console.log(Math.ceil(a)) // 4

        // 2.3 四舍五入
        console.log(Math.round(2.6))//3
    </script>

1.4 Math取随机数

 <script>
        // random方法进行随机取值
        // 随机值的取值为 0 <= x < 1
        console.log(Math.random())
        // 能不能获取到一个随机数 10 - 100
        // [0 - 1) * 90 = [0 - 90)
        console.log(Math.floor((Math.random() * (100 - 10)) + 10))

        // 封装成了一个函数
        function getRandom(min, max)  {
            return Math.floor((Math.random() * (max - min + 1)) + min)
        }

        // 随机点名
        var arr = ["曹鑫", "吉喆", "张嘉译"];
        console.log(arr[getRandom(0, arr.length - 1)])
    </script>

1.5 猜数字游戏

<script>
        // 猜数字
        // 随机生成 1 - 10之间的整数,
        // 2.需要猜数字,一直猜,如果正确就提示正确了,如果不正确就一直猜。
        // 如果数字大了,提示大 小了提示小了。

        function getRandom(max, min) {
            return Math.floor(Math.random() * (max - min + 1) + min)
        }
        var randomNum = getRandom(1, 10);
        while(1) {
            var num = prompt("请输入您猜想的数字: (1-10之间)")
            if(num > randomNum) {
                alert("您猜的数字大了")
            }else if(num < randomNum) {
                alert("您猜的数字小了")
            }else {
                alert("猜对了")
                break;
            }
        }
        // 要求用户猜 1~50之间的一个数字 但是只有 10次猜的机会
    </script>