const randomBoolean = () => Math.random() >= 0.5;
console.log(randomBoolean());
// true:50%; false:50%
const isWeekday = (date) => date.getDay() % 6 !== 0;
console.log(isWeekday(new Date(2021, 0, 11)));
// true (Monday)
console.log(isWeekday(new Date(2021, 0, 10)));
// false (Sunday)
const reverse = str => str.split('').reverse().join('');
reverse('hello world');
// 'dlrow olleh'
const isBrowserTabInView = () => document.hidden;
isBrowserTabInView();
// Result: returns true or false depending on if tab is in view / focus
const isEven = num => num % 2 === 0;
console.log(isEven(2));
//true
console.log(isEven(3));
// false
const timeFromDate = date => date.toTimeString().slice(0, 8);
console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0)));
// "17:30:00"
console.log(timeFromDate(new Date()));
//当前时间
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
// Examples
toFixed(36.745896214, 1); // 36.7
toFixed(36.745896214, 2); // 36.74
toFixed(36.745896214, 3); // 36.745
toFixed(36.745896214, 4); // 36.7458
toFixed(36.745896214, 5); // 36.74589
toFixed(36.745896214, 6); // 36.745896
const elementIsInFocus = (el) => (el === document.activeElement);
elementIsInFocus(anyElement)
//true -获焦,false -不是获焦
const touchSupported = () => {
('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
}
console.log(touchSupported());
// Result: will return true if touch events are supported, false if not
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevice);
// IE 不支持 scrollTo() 方法
const goToTop = () => window.scrollTo(0, 0);
goToTop();
const average = (...args) => args.reduce((a, b) => a + b) / args.length;
average(1, 2, 3, 4);
// 2.5
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
// Examples
celsiusToFahrenheit(15); // 59
celsiusToFahrenheit(0); // 32
celsiusToFahrenheit(-20); // -4
fahrenheitToCelsius(59); // 15
fahrenheitToCelsius(32); // 0
网友评论