JS学习笔记10之Math对象

-->Math对象 常用属性和方法
-->使用Math对象制作相应的效果

 

Math对象用于执行数学任务

一、Math对象的属性

二、Math对象的方法

三、常用属性方法

Math.PI ----------------返回圆周率3.14 ...
Math.ceil(x) ------------对数值x进行向上取整
Math.floor(x) -----------对数值x进行向下取整
Math.round(x) ----------对数值x进行四舍五入
Math.min(a,b,c...) -------返回abc...中的最小值
Math.max(a,b,c...) -------返回abc...中的最大值
Math.random() --------返回介于0 ~ 1 之间的随机数

 1 <script>
 2 /*Math.PI ----返回圆周率3.14 */
 3     var a=Math.PI;
 4     console.log('a='+a);//a=3.141592653589793
 5 /*Math.ceil(x) ------对数值x进行向上取整*/
 6     console.log('b='+Math.ceil(a));//b=4
 7 /*Math.floor(x) ------对数值x进行向下取整*/
 8     console.log('c='+Math.floor(a));//c=3
 9 /*Math.round(x) -----对数值x进行四舍五入*/
10     console.log('d='+Math.round(18.500000));//d=19
11     console.log('d='+Math.round(18.499999));//d=18
12 /*Math.min(a,b,c...) ----返回abc...中的最小值*/
13     console.log('min='+Math.min(0,2,5,67,335,63,99));//min=0
14 /*Math.max(a,b,c...) ---返回abc...中的最大值*/
15     console.log('max='+Math.max(0,2,5,67,335,63,99));//max=335
16 /*Math.random() -----返回介于0 ~ 1 之间的随机数*/
17     console.log('0~1随机数是'+Math.random());//0.22180383793467096
18 </script>

 

   四、使用Math对象制作相应的效果 

1、10个1~20的不重复的随机数

 1 <body>
 2     <h1 id="con1">10个1~20的不重复的随机数</h1>
 3 <script>
 4     var con1=document.getElementById('con1');
 5     var arr=[];
 6     for (var i = 0; i < 10; i++) {
 7         var r=parseInt(Math.random()*20)+1;
 8         arr.push(r);
 9         console.log(arr);
10         for (var j = 0; j < i; j++) {
11             if (arr[j]==r) {
12                 arr.pop();
13                 i--;
14                 break;
15             }
16         }
17     }
18     con1.innerText=arr;
19     console.log(arr.length);
20 </script>
21 </body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值