This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Traverse up the DOM tree to find an element with data-place-id attribute | |
let targetElement = e.target; | |
while (targetElement && !targetElement.dataset.placeId) { | |
targetElement = targetElement.parentElement; | |
} | |
// Retrieve the data-place-id attribute value | |
const placeId = targetElement ? targetElement.dataset.placeId : undefined; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length; | |
average([1,9,18,36]) //16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1); | |
rgbToHex(255, 255, 255); | |
// #ffffff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dayDiff = (date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000); | |
dayDiff(new Date("2021-10-21"), new Date("2022-02-12")) | |
// Result: 114 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9; | |
fahrenheitToCelsius(50); | |
// 10 | |
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32; | |
celsiusToFahrenheit(100) | |
// 212 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isWeekday = (date) => date.getDay() % 6 !== 0; | |
isWeekday(new Date()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getSelectedText = () => window.getSelection().toString(); | |
getSelectedText(); //Return selected content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const copyText = async (text) => await navigator.clipboard.writeText(text) | |
copyText('One Line of Code Front End World') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const judgeDeviceType = | |
() => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(navigator.userAgent) ? 'Mobile' : 'PC'; | |
judgeDeviceType() | |
// PC | Mobile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isTabActive = () => !document.hidden;isTabActive() | |
// true|false |
NewerOlder