文章目录
一、Math对象
1.Math数学对象 不是一个构造函数,所以我们不需要new 来调用 而是直接使用里面的属性和方法即可
二、使用步骤
1.Math绝对值
//1.绝对值方法
console.log(Math.abs(1));
console.log(Math.abs(-1));
console.log(Math.abs('-1'));//会把字符串型-1转换为数字型
console.log(Math.abs('pink'));//NaN
2.三个取整方法
//2.三个取整的方法
//Math.floor() 向下取整
console.log(Math.floor(1.1));//1
console.log(Math.floor(1.9));//1
//Math.ceil() 向上取整 往最大取值
console.log(Math.ceil(1.9));//2
console.log(Math.ceil(1.1))//2
//Math.round() 四舍五入
console.log(Math.round(1.1));//1
console.log(Math.round(1.9));//2
3.随机数方法
//1.获取0=<x<1之间的随机数
console.log(Math.random());
//2.两个数之间的随机数
function getRandom(min,max){
return Math.floor(Math.random()*(max-min+1))+min;
}
console.log(getRandom(1,10));
//3.随机点名
var arr=['张三','张三丰','李四','jack'];
console.log(arr[getRandom(0,3)]);