1. 最大值和最小值

min()和max()方法用于确定一组数值中的最小值和最大值,对于数组中最大最小值可以通过变通的方式,如下:

  1. //方法1
  2. Math.max(...arr)
  3. //方法2
  4. Math.max.apply(Math,arr)

2. 舍入方法

Math.ceil():向上取整,总是将数值向上舍入为最接近的整数。
Math.floor():向下取整,将数值向下舍入为最接近的整数。
Math.round():标准舍入,将数值四舍五入为最接近的整数。

3. 随机数

Math.random()方法返回大于等于0小于1的一个随机数。套用以下方法可以指定区间内的随机数,代码如下:

  1. function selectFrom(lowerValue,upperValue) {
  2. var choices = upperValue - lowerValue + 1
  3. return Math.floor(Math.random()*choices + lowerValue)
  4. }