Skip to main content

Get timezone as a string

Nov 19, 2022CodeCatch
Loading...

More JavaScript Posts

luhnCheck implementation

Nov 19, 2022CodeCatch

0 likes • 1 view

const luhnCheck = num => {
let arr = (num + '')
.split('')
.reverse()
.map(x => parseInt(x));
let lastDigit = arr.splice(0, 1)[0];
let sum = arr.reduce(
(acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val *= 2) > 9 ? val - 9 : val)),
0
);
sum += lastDigit;
return sum % 10 === 0;
};
luhnCheck('4485275742308327'); // true
luhnCheck(6011329933655299); // true
luhnCheck(123456789); // false

return array head

Nov 19, 2022CodeCatch

0 likes • 0 views

const head = arr => (arr && arr.length ? arr[0] : undefined);
head([1, 2, 3]); // 1
head([]); // undefined
head(null); // undefined
head(undefined); // undefined

quick sort

Nov 19, 2022CodeCatch

0 likes • 0 views

const quickSort = arr => {
const a = [...arr];
if (a.length < 2) return a;
const pivotIndex = Math.floor(arr.length / 2);
const pivot = a[pivotIndex];
const [lo, hi] = a.reduce(
(acc, val, i) => {
if (val < pivot || (val === pivot && i != pivotIndex)) {
acc[0].push(val);
} else if (val > pivot) {
acc[1].push(val);
}
return acc;
},
[[], []]
);
return [...quickSort(lo), pivot, ...quickSort(hi)];
};
quickSort([1, 6, 1, 5, 3, 2, 1, 4]); // [1, 1, 1, 2, 3, 4, 5, 6]

localStorage Cookie Consent

Oct 9, 2023Helper

0 likes • 196 views

import React, { useState, useEffect } from 'react';
import Link from 'next/link';
export default function CookieBanner() {
// Initialize with a default value (false if not previously set in localStorage)
const [cookieConsent, setCookieConsent] = useState(() =>
localStorage.getItem('cookieConsent') === 'true' ? true : false
);
useEffect(() => {
const newCookieConsent = cookieConsent ? 'granted' : 'denied';
window.gtag('consent', 'update', {
analytics_storage: newCookieConsent
});
localStorage.setItem('cookieConsent', String(cookieConsent));
}, [cookieConsent]);
return !cookieConsent && (
<div>
<div>
<p>
We use cookies to enhance the user experience.{' '}
<Link href='/privacy/'>View privacy policy</Link>
</p>
</div>
<div>
<button type="button" onClick={() => setCookieConsent(false)}>Decline</button>
<button type="button" onClick={() => setCookieConsent(true)}>Allow</button>
</div>
</div>
)
}

Subsets of an array

Nov 19, 2022CodeCatch

0 likes • 2 views

const getSubsets = arr => arr.reduce((prev, curr) => prev.concat(prev.map(k => k.concat(curr))), [[]]);
// Examples
getSubsets([1, 2]); // [[], [1], [2], [1, 2]]
getSubsets([1, 2, 3]); // [[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

Mee6 Spam Calculator

Mar 10, 2022LeifMessinger

0 likes • 1 view

//Use at https://mee6.xyz/leaderboard/732262089447702588
//Higher the spammy stat, the more spammy that person is.
//This is because mee doesn't give experience to people who post more comments in a minute.
function statBlock(title, value){
let elm = document.createElement("div");
elm.className = "leaderboardPlayerStatBlock";
let titleElm = document.createElement("div");
titleElm.className = "leaderboardPlayerStatName";
titleElm.textContent = title;
let valueElm = document.createElement("div");
valueElm.className = "leaderboardPlayerStatValue";
valueElm.textContent = value;
elm.appendChild(titleElm);
elm.appendChild(valueElm);
elm.remove = function(){
this.parentElement.removeChild(this);
}
return elm;
}
for(let player of Array.from(document.getElementsByClassName("leaderboardPlayer"))){
if(player.spamminess) player.spamminess.remove();
let messages = null;
let experience = null;
const statBlockArray = Array.from(player.querySelectorAll(".leaderboardPlayerStatBlock"));
for(let statBlock of statBlockArray){
const statName = statBlock.querySelector(".leaderboardPlayerStatName").textContent;
const text = statBlock.querySelector(".leaderboardPlayerStatValue").textContent;
const number = ((text.includes("k"))? (text.replace("k","") * 1000.0) : +(text));
if(statName.includes("MESSAGES")){
messages = number;
}else if(statName.includes("EXPERIENCE")){
experience = number;
}
}
const stats = player.querySelector(".leaderboardPlayerStats");
const messagesElement = stats.firstChild;
const spamminess = ((messages/experience)*2000.0).toFixed(2);
player.spamminess = stats.insertBefore(statBlock("SPAMMINESS", spamminess), messagesElement);
}