Math对象

JavaScript 中的对象分为3种:

  • 自定义对象,var obj = {}
  • 内置对象, Math, Date,Array、String等
  • 浏览器对象 BOM

前面两种对象是JS 基础 内容,属于 ECMAScript;

第三个浏览器对象BOM Web API 讲解

介绍

Math对象不是构造函数,它具有数学常数和函数的属性和方法

不需要new 来调用

直接使用里面的属性和方法

最大值/最小值

console.log(Math.PI); // 一个属性 圆周率   //3.141592653589793

// 方法

console.log(Math.max(-1, -10))            //-1

console.log(Math.max(1, 99, 3))           //99

console.log(Math.max(1, 99, '张三'))     //NaN

console.log(Math.max())                       // -Infinity

console.log(Math.min(1, 2))                  //1

console.log(Math.min())                        // Infinity

 案例: 封装自己的数学对象 - 里面有PI, 最大值,最小值

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, 5, 9));
console.log(myMath.min(1, 5, 9));

 绝对值/取整

1.绝对值方法

console.log(Math.abs( 1 ))       // 1

console.log(Math.abs( -1 ))       //1

console.log(Math.abs( '张三' ))   //NaN  

2.三个取整方式

(1)  Math.floor() 地板 向下取整 往最小了取值(取坐标轴的左边整数)

console.log(Math.floor( 1.1 ))    //1   

console.log(Math.floor( 1.9 ))    //1

(2) Math.ceil() ceil 天花板 向上取整 往最大了取值(取坐标轴的右边整数)

 console.log(Math.ceil( 1.1 ))   //2

 console.log(Math.ceil( 1.9 ))  //2

 console.log(Math.ceil( 1.5 ))   //2

(3)Math.round() 四舍五入 其他数字都是四舍五入

console.log(Math.round( 1.1 ))    //1

console.log(Math.round( 1.5 ))    //2

console.log(Math.round( 1.9 ))    //2

console.log(Math.round( -1.1 ))   //-1

console.log(Math.round( -1.5 ))   //-1

随机数

Math对象随机数方法 random( ) 返回一个随机的小数 0  <= x  < 1  [0,1)

Math.random();//这个方法里面不跟参数

 得到一个两数之间的随机整数

function getRandom (min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
}
  // 传入1,,10,过程如下:
  // Math.floor( 0 =< x < 1 * (max - min + 1) ) + min;
  // Math.floor( 0 =< x < 1 * 10 ) + min;
  // Math.floor( 0 =< x < 10  ) + min; // 这个是带小数的范围
  // 0 =< x <= 9  + 1; // 取过floor之后就是整数
  // 1 =< x <= 10  // [1,10]

 公式:(可直接记忆)

function getRandom (min, max) {

return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值

}

案例:猜数字游戏

1.随机生成一个1~10 的整数 我们需要用到 Math.random() 方法。

2.需要一直猜到正确为止,所以需要一直循环

3.while 循环更简单

4.核心算法:使用 if else if 多分支语句来判断大于、小于、等于。

 function getRadom(min, max) {

            return Math.floor(Math.random() * (max - min + 1) + min)

        }

        var random = getRadom(1, 10)

        // console.log(random);

        while (true) {

            var num = prompt('请输入1-10之间任意一个数')

            if (num < random) {

                alert('您输入的数小了')

            } else if (num > random) {

                alert('您输入的数大了')

            } else {

                alert('对了,真棒')

                break   //退出整个循环

            }

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值