JS学习笔记10之Math对象

来源:互联网 发布:门捷列夫没得奖 知乎 编辑:程序博客网 时间:2024/05/17 04:45
-->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=1911     console.log('d='+Math.round(18.499999));//d=1812 /*Math.min(a,b,c...) ----返回abc...中的最小值*/13     console.log('min='+Math.min(0,2,5,67,335,63,99));//min=014 /*Math.max(a,b,c...) ---返回abc...中的最大值*/15     console.log('max='+Math.max(0,2,5,67,335,63,99));//max=33516 /*Math.random() -----返回介于0 ~ 1 之间的随机数*/17     console.log('0~1随机数是'+Math.random());//0.2218038379346709618 </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
原创粉丝点击