当前位置:   article > 正文

JavaScript中Math数学对象_使用math对象的属性及随机数方法和string对象的相关方法,在页面中生成一个四位验

使用math对象的属性及随机数方法和string对象的相关方法,在页面中生成一个四位验

Math数学对象

Math 对象用于执行数学任务

Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。

1.1 Math 对象属性

Math 对象属性

使用属性 PI 圆周率 的写法  Math.PI

编写一个函数,实现已知半径是5,求圆的面积。

  1. // 编写一个函数,实现已知半径是5,求圆的面积。
  2. function squ(r){
  3. return Math.PI * r * r
  4. }
  5. document.write('半径是5,圆的面积是:'+ squ(5) )

 预览:

1.2 数学对象的方法

Math 对象方法

 

1、abs() 取一个数的绝对值

使用方法Math.abs() 取一个数的绝对值

console.log(Math.abs(-100));//输出100

2、Math.round()四舍五入 取数

  1. console.log(Math.round(5.6));//输出6
  2. console.log(Math.round(5.1));//输出5

 3、Math.ceil()向上进一 注意:只要 小数部分是大于0的数,整个数 都会向上 进一

  1. console.log(Math.ceil(5.1));//输出6
  2. console.log(Math.ceil(5.01));//输出6

4、Math.floor()向下舍一注意:不管小数部分,只取整数部分 ( 方便理解的记忆方法:小数点后面不管是什么,我不要!)

  1. console.log(Math.floor(7.6));//输出7
  2. console.log(Math.floor(7.1));//输出7
  3. console.log(Math.floor(Math.PI));//输出3

 5、Math.random() 0-1随机数(无限接近0,或 无限接近1的随机数)返回介于 0(包含) ~ 1(不包含) 之间的一个随机数

console.log(Math.random());

预览:

取得介于 min 到 max 之间的一个随机数万能公式:(不包括max)

生成“指定区间”内的随机数(min(包含)~ max(不包含)之间的数字)

Math.floor(Math.random() * (max - min) + min)

万能公式:(包括max)

生成“指定区间”内的随机数(返回 min(包含)~ max(包含)之间的数字)

Math.floor(Math.random() * (max - min + 1) + min)

0-5随机数(不包括5)

  1. var r1=Math.floor(Math.random()*(5-0) + 0)
  2. console.log(r1);

0-5随机数(包括5)

  1. let r2 = Math.floor(Math.random()*(5-0+1)+0)
  2. console.log(r2)

 预览:

11-15随机数(不包括15)

  1. var r3=Math.floor(Math.random()*(15-11) + 11)
  2. console.log(r3);

 11-15随机数(包括15)

  1. var r4=Math.floor(Math.random()*(15-11+1) + 11)
  2. console.log(r4);

 预览:

编写一个函数,实现的功能是生成20-40随机数(包括40)。

  1. function random(max,min){
  2. return Math.floor(Math.random()*(max-min+1)+min)
  3. }
  4. console.log(random(40,20));

 预览:

让数组中某个位置上的内容随机输出

  1. var arr=['小余','小马','小杨','小许','小王','小天'];
  2. //数组的下标最小值0
  3. //数组的下标最大值arr.length-1
  4. var random=Math.floor (Math.random()*(arr.length-1-0+1) + 0);
  5. console.log(random);
  6. document.querySelector('#uname').innerHTML=arr[random];

 预览:

编写一个函数,随机生成四位数验证码。(四位数是0-9)

  1. //编写一个函数,随机生成四位数验证码(0-9)
  2. var str='0123456789';
  3. var code='';
  4. function randomCode(){
  5. //索引的最小值,索引的最大值str.length-1
  6. for(var i=0; i<4;i++){
  7. var random=Math.floor(Math.random()*(str.length-1-0+1)+0)
  8. code +=str[random]
  9. }
  10. return code
  11. }
  12. var r=randomCode()
  13. console.log(r);

 预览:

编写一个函数,随机生成四位数验证码。(四位数是0-9,a-z,A-Z)

  1. let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  2. let code = '';
  3. function randomCode(){
  4. for(var i=0;i<4;i++){
  5. var random=Math.floor(Math.random()*(str.length-1-0 + 1)+0)
  6. console.log(random)
  7. code +=str[random]
  8. }
  9. return code
  10. }
  11. document.write(randomCode(str))

 预览:

优化上面的代码,优化结果如下所示:

  1. let str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  2. function getCode(str){
  3. let code='';
  4. for(let i=0;i<4;i++){
  5. var random=Math.floor(Math.random()*(str.length-1-0 + 1)+0)
  6. code +=str[random]
  7. }
  8. return code
  9. }
  10. document.write(getCode(str))

 注意:以后写 随机验证码,按照优化后的编写。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/54556
推荐阅读
相关标签
  

闽ICP备14008679号