JavaScript中Math数学对象

Math数学对象

Math 对象用于执行数学任务

Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。

1.1 Math 对象属性

Math 对象属性

使用属性 PI 圆周率 的写法  Math.PI

编写一个函数,实现已知半径是5,求圆的面积。

// 编写一个函数,实现已知半径是5,求圆的面积。
function squ(r){
    return Math.PI * r * r
}
document.write('半径是5,圆的面积是:'+ squ(5) )

 预览:

1.2 数学对象的方法

Math 对象方法

 

1、abs() 取一个数的绝对值

使用方法Math.abs() 取一个数的绝对值

console.log(Math.abs(-100));//输出100

2、Math.round()四舍五入 取数

console.log(Math.round(5.6));//输出6
console.log(Math.round(5.1));//输出5

 3、Math.ceil()向上进一 注意:只要 小数部分是大于0的数,整个数 都会向上 进一

console.log(Math.ceil(5.1));//输出6
console.log(Math.ceil(5.01));//输出6

4、Math.floor()向下舍一注意:不管小数部分,只取整数部分 ( 方便理解的记忆方法:小数点后面不管是什么,我不要!)

console.log(Math.floor(7.6));//输出7
console.log(Math.floor(7.1));//输出7
console.log(Math.floor(Math.PI));//输出3

 5、Math.random() 0-1随机数(无限接近0,或 无限接近1的随机数)返回介于 0(包含) ~ 1(不包含) 之间的一个随机数

console.log(Math.random());

预览:

取得介于 min 到 max 之间的一个随机数万能公式:(不包括max)

生成“指定区间”内的随机数(min(包含)~ max(不包含)之间的数字)

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

万能公式:(包括max)

生成“指定区间”内的随机数(返回 min(包含)~ max(包含)之间的数字)

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

0-5随机数(不包括5)

var r1=Math.floor(Math.random()*(5-0) + 0)
console.log(r1);

0-5随机数(包括5)

let r2 = Math.floor(Math.random()*(5-0+1)+0) 
console.log(r2)

 预览:

11-15随机数(不包括15)

var r3=Math.floor(Math.random()*(15-11) + 11)
console.log(r3);

 11-15随机数(包括15)

var r4=Math.floor(Math.random()*(15-11+1) + 11)
console.log(r4);

 预览:

编写一个函数,实现的功能是生成20-40随机数(包括40)。

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

}
console.log(random(40,20));

 预览:

让数组中某个位置上的内容随机输出

var arr=['小余','小马','小杨','小许','小王','小天'];
//数组的下标最小值0
//数组的下标最大值arr.length-1
var random=Math.floor (Math.random()*(arr.length-1-0+1) + 0);
console.log(random);
document.querySelector('#uname').innerHTML=arr[random];

 预览:

编写一个函数,随机生成四位数验证码。(四位数是0-9)

 //编写一个函数,随机生成四位数验证码(0-9)
        var str='0123456789';
        var code='';
        function randomCode(){
           
            //索引的最小值,索引的最大值str.length-1
             for(var i=0; i<4;i++){
                 var random=Math.floor(Math.random()*(str.length-1-0+1)+0)
                code +=str[random]

             }
             return code
        }
        var r=randomCode()
        console.log(r);

 预览:

编写一个函数,随机生成四位数验证码。(四位数是0-9,a-z,A-Z)

let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
let code = '';
function randomCode(){
for(var i=0;i<4;i++){
var random=Math.floor(Math.random()*(str.length-1-0 + 1)+0)
console.log(random)
code +=str[random]
}
return code
}
document.write(randomCode(str))

 预览:

优化上面的代码,优化结果如下所示:

let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function getCode(str){
let code='';
for(let i=0;i<4;i++){
var random=Math.floor(Math.random()*(str.length-1-0 + 1)+0)

code +=str[random]
}
return code
}

document.write(getCode(str))

 注意:以后写 随机验证码,按照优化后的编写。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值