Skip to content

Instantly share code, notes, and snippets.

View oguzhancvdr's full-sized avatar
:electron:
Focusing

Oguzhan C. oguzhancvdr

:electron:
Focusing
View GitHub Profile
// 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;
const average = (arr) => arr.reduce((a, b) => a + b) / arr.length;
average([1,9,18,36]) //16
const rgbToHex = (r, g, b) => "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
rgbToHex(255, 255, 255);
// #ffffff
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
const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;
fahrenheitToCelsius(50);
// 10
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;
celsiusToFahrenheit(100)
// 212
const isWeekday = (date) => date.getDay() % 6 !== 0;
isWeekday(new Date());
const getSelectedText = () => window.getSelection().toString();
getSelectedText(); //Return selected content
const copyText = async (text) => await navigator.clipboard.writeText(text)
copyText('One Line of Code Front End World')
const judgeDeviceType =
() => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|OperaMini/i.test(navigator.userAgent) ? 'Mobile' : 'PC';
judgeDeviceType()
// PC | Mobile
const isTabActive = () => !document.hidden;isTabActive()
// true|false