1. 获取一个随机的布尔值
const random = function(){
return Math.random() >= 0.5
}
console.log(random());
2. 检查日期是不是工作日
const isWeek = function(date){
return date.getDay() % 6 !== 0;
}
console.log(isWeek(new Date(2021,04,04)));
3. 反转字符串
const reverseStr = function(str){
return str.split(‘‘).reverse().join(‘‘)
}
console.log(reverseStr(‘hello‘));
4. 检查当前tab页是否在前台
const isBroTabInView = () => document.hidden;
console.log(isBroTabInView())
5. 检查数字是否为奇数
const isEven = num => num % 2 === 0;
console.log(isEven(3))
6. 从日期中获取时间
const timeFormDate = date => date.toTimeString().slice(0,8);
console.log(timeFormDate(new Date())) // 当前的时间
7. 检查元素当前是否为聚焦状态
const isFocus = function(ele){
return ele === document.activeElement;
}
isFocus(anyElement)
8. 检查当前用户是不是苹果设备
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevice)
9. 滚动到页面顶部
const goToTop = () => window.scrollTo(0,0);
10. 获取所有参数平均数
const average = (...args) => args.reduce((a,b)=>a+b) / args.length;
average(1,2,3,4)
11. 转换华氏度/摄氏度
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;