js数组方法

2021-08-09  本文已影响0人  小李不小
1.Math.round():根据“round”的字面意思“附近、周围”,可以猜测该函数是求一个附近的整数,看下面几个例子就明白。
小数点后第一位<5
正数:Math.round(11.46)=11
负数:Math.round(-11.46)=-11
 
小数点后第一位>5
正数:Math.round(11.68)=12
负数:Math.round(-11.68)=-12
2.Math.ceil():根据“ceil”的字面意思“天花板”去理解;

例如:

Math.ceil(11.46)=Math.ceil(11.68)=Math.ceil(11.5)=12

3.Math.floor():根据“floor”的字面意思“地板”去理解;

例如:

Math.floor(11.46)=Math.floor(11.68)=Math.floor(11.5)=11
Math.floor(-11.46)=Math.floor(-11.68)=Math.floor(-11.5)=-12

封装 取数组3-5位的之间的数字

let a=[1,2,3,4,5]
function arrrandomval(array,start=1,end){
    console.log(arguments)
    end=end?end:array.length;
    console.log('end----',end)
    start--;
    const index =start + Math.floor(Math.random() * (end-start))
    return array[index];

}


console.log(arrrandomval(a,3,5))

取(n-x)之间的随机数 公式
n+Math.floor(Math.random()*(x-n))

上一篇 下一篇

猜你喜欢

热点阅读