Js函数的数学对象(Math)

数学对象提供基本的数学函数和常数

数学对象不需要使用new对象

ceil()  返回的是大于该数字的最小整数

floor()   console.log(Math.floor(12.7))//12  

round()  将数字进行四舍五入

max()   求最大值 两个及多个数中的最大值

pow(2,3)  2的三次方

random()  随机产生0-1之间的任意数字【0,1】

<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>
		<input type="text" name="result" id="result" value="" />
		<input type="button"  id="cai" value="猜数字" />
		<script type="text/javascript">
			//Math对象
			console.log(Math.ceil(12.3))//13 返回的是大于该数字的最小整数
			console.log(Math.floor(12.7))//12  返回的是小于该数字的最大整数
			console.log(Math.round(12.6))//13  将数字进行四舍五入
			console.log(Math.max(12,30,15,100))//求最大值 两个及多个数中的最大值
			console.log(Math.pow(2,3))//2的三次方
			console.log(Math.random())//随机产生0-1之间的任意数字【0,1】
			//随机出0--10之间的任意数字
			console.log(Math.floor(Math.random()*11))
			//猜数字游戏
			var num=Math.floor(Math.random()*11)
			console.log(num)
			document.getElementById("cai").onclick=function(){
				//现货区文本框中输入的数字
				var result=Number(document.getElementById("result").value)
				if(result>num){
					alert("你猜的数字太大了")
				}else if(result<num){
					alert("你猜的数字太小了")
				}else{
					alert("恭喜你猜对了")
				}
			}
		</script>
	</body>
</html>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值