JS面试手写:几个简单的工具函数

100 阅读1分钟
  1. 随机排序数组
// 利用0.5 - Math.random返回不确定的正负数实现乱序排列
const shuffleArray = (arr) => arr.sort(() => 0.5 - Math.random())
  1. 在给定的数字范围内生成随机的数字
const random = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
  1. 计算间隔的天数
// 传入的date为Date类型
const daysDiff = (date1, date2) => Math.ceil(Math.abs(date1 - date2) / 8640000)