13 JavaScript one-line programs to make you look like an expert

13 JavaScript one-line programs to make you look like an expert

13 JavaScript one-line programs to make you look like an expert

JavaScript can do a lot of fun things, from complex frameworks to processing APIs, there are too many things to learn. However, it also allows us to do amazing things with just one line.

1. Get a random Boolean value ( true/ false)

This function uses the Math.random()method to return a boolean value ( trueor false). Math.randomCreate a 0to a 1random number between, as long as we check whether it is above or below 0.5, there is a 50% chance of getting trueor false.

const randomBoolean = () => Math.random() >= 0.5;
console.log(randomBoolean()); 

2. Check if the provided date is a working day

Using this method, we can check whether the date provided in the function is a weekday or a weekend day.

const isWeekday = (date) => date.getDay() % 6 !== 0;

console.log(isWeekday(new Date(2021, 7, 6)));
// true  

console.log(isWeekday(new Date(2021, 7, 7)));
// false  

3. Reverse the string

There are several different ways to reverse a string. This is the simplest one, use split(), reverse()and join()method.

const reverse = str => str.split('').reverse().join('');
reverse('hello world');     
// 'dlrow olleh' 

4. Check if the current label is hidden

Document.hidden(Read-only attribute) returns a boolean value, indicating truewhether the page is ( ) or not ( false) hidden.

const isBrowserTabInView = () => document.hidden;
isBrowserTabInView(); 

Off-site: I accidentally discovered that the countdown of iQiyi's ad playback time is when the current tab is activated. When you leave the current tab, the countdown stops. Baidu found document.hiddenthis stuff.

document.hiddenIt is a compatibility problem when it is h5 newly added api.

var hidden
if (typeof document.hidden !== "undefined") {
    hidden = "hidden";
} else if (typeof document.mozHidden !== "undefined") {
    hidden = "mozHidden";
} else if (typeof document.msHidden !== "undefined") {
    hidden = "msHidden";
} else if (typeof document.webkitHidden !== "undefined") {
    hidden = "webkitHidden";
}
console.log(""Whether the current page is hidden":" + document[hidden])

5. Check if a number is even or odd

const isEven = num => num % 2 === 0;
console.log(isEven(2));
// true
console.log(isEven(3));
// false 

6. Get the time from a date

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()));
// // print the current time 

7. Keep n decimal places

const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);
// Examples
toFixed(25.198726354, 1); // 25.1 toFixed(25.198726354, 2); // 25.19 toFixed(25.198726354, 3); // 25.198 toFixed(25.198726354, 4); // 25.1987 toFixed(25.198726354, 5); // 25.19872 toFixed(25.198726354, 6); // 25.198726 

8. Check if any element is currently in focus

We can use document.activeElementattributes to check whether an element is currently in focus.

const elementIsInFocus = (el) => (el === document.activeElement); elementIsInFocus(anyElement) // If it is in focus, it returns true, if it is not in focus, it returns false

9. Check whether the current browser supports touch events

const touchSupported = () => {
  ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
}
console.log(touchSupported());
// If touch events are supported, it will return true, if not, it will return false.

10. Check if the current browser is on the Apple device

const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
console.log(isAppleDevice); 

11. Scroll to the top of the page

const goToTop = () => window.scrollTo(0, 0);
goToTop(); 

12. Get the average value of the parameter

const average = (...args) => args.reduce((a, b) => a + b) / args.length;
average(1, 2, 3, 4);
// 2.5 

13. Fahrenheit/Celsius conversion

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

What's Your Reaction?

like
0
dislike
0
love
0
funny
0
angry
0
sad
0
wow
0