JavaScript之Math对象

简要
JavaScript中的对象分为3种:自定义对象、内置对象,浏览器对象。

  • 前面两种对象是JS基础内容,属于ECMAScript;浏览器对象属于JS独有的。
  • 内置对象指JS语言自带的一些对象,这些对象供开发者使用,并提供了一些常用的或是最基本而必要的功能(属性和方法)。
  • 内置对象最大的优点:帮助我们快速开发。
  • JavaScript提供了多个内置对象:Math、Date 、Array、string等。
  • 因内置对象比较多,可自行查以下的链接。

查文档
https://developer.mozilla.org/zh-CN/
如何学习对象中的方法
1.查阅该方法的功能。
在这里插入图片描述
2.查看里面参数的意义和类型。
3.查看返回值的意义和类型。

常用的Math对象
1、最大值之Math.max()

console.log(Math.max(1,99,3));//99
console.log(Math.max(-1,-10));//-1
console.log(Math.max(1,99,‘wusheng’));//NaN
console.log(Math.max());//-Infinity

栗子:利用对象封装自己的数学对象,里面有最大值和最小值。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
	// Math对象,不是一个构造函数,不需new来调用,而是直接使用里面的属性和方法
    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));
    </script>
</body>
</html>

2、绝对值之Math.abs()

console.log(Math.abs(1));//1
console.log(Math.abs(-1));//1
console.log(Math.abs(’-1’));//隐式转换,会把字符串-1转换为数字
console.log(Math.abs(‘jinjin’));//NaN

3、三个取整方法
(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

(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

4、随机数之Math.random()

//1、Math对象随机数方法 random()返回一个随机的小数 0=<x<1
//2、方法里面不跟参数
//3、代码验证
console.log(Math.random());
//4、我们想要得到两个数之间的随机数 并且 包含着这两个整数
// Math.floor(Math.random()*(max-min+1))+min;
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值
}

console.log(getRandom(1,10))
//5、随机点名
var arr =[‘小明’,‘小红’,‘小花’,‘小华’,‘笑笑’];
console.log(arr[getRandom(0,arr.length-1)]);

猜数字游戏
1、随机生成一个1~10的整数 我们需要用到Math.random()方法
2、需要一直猜到正确为止,所以需要一直循环
3、while 循环更简单
4、核心算法:使用if else if多分支语句来判断大于、小于、等于。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script>
        function getRandom(min, max) {
			  return Math.floor(Math.random() * (max - min + 1)) + min; //含最大值,含最小值 
		}
		var random=getRandom(1,10);
		while(true){
			var num =prompt('你来猜? 输入1~10之间的一个数字');
			if (num>random) {
				alert('你猜大了')
			}else if(num<random){
				alert('你猜小了')
			}else{
				alert('哇哦!猜对了');
				break;
			}
		}
	</script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值