Loading...
More JavaScript Posts
class DoublyLinkedList {constructor() {this.nodes = [];}get size() {return this.nodes.length;}get head() {return this.size ? this.nodes[0] : null;}get tail() {return this.size ? this.nodes[this.size - 1] : null;}insertAt(index, value) {const previousNode = this.nodes[index - 1] || null;const nextNode = this.nodes[index] || null;const node = { value, next: nextNode, previous: previousNode };if (previousNode) previousNode.next = node;if (nextNode) nextNode.previous = node;this.nodes.splice(index, 0, node);}insertFirst(value) {this.insertAt(0, value);}insertLast(value) {this.insertAt(this.size, value);}getAt(index) {return this.nodes[index];}removeAt(index) {const previousNode = this.nodes[index - 1] || null;const nextNode = this.nodes[index + 1] || null;if (previousNode) previousNode.next = nextNode;if (nextNode) nextNode.previous = previousNode;return this.nodes.splice(index, 1);}clear() {this.nodes = [];}reverse() {this.nodes = this.nodes.reduce((acc, { value }) => {const nextNode = acc[0] || null;const node = { value, next: nextNode, previous: null };if (nextNode) nextNode.previous = node;return [node, ...acc];}, []);}*[Symbol.iterator]() {yield* this.nodes;}}const list = new DoublyLinkedList();list.insertFirst(1);list.insertFirst(2);list.insertFirst(3);list.insertLast(4);list.insertAt(3, 5);list.size; // 5list.head.value; // 3list.head.next.value; // 2list.tail.value; // 4list.tail.previous.value; // 5[...list.map(e => e.value)]; // [3, 2, 1, 5, 4]list.removeAt(1); // 2list.getAt(1).value; // 1list.head.next.value; // 1[...list.map(e => e.value)]; // [3, 1, 5, 4]list.reverse();[...list.map(e => e.value)]; // [4, 5, 1, 3]list.clear();list.size; // 0
const heapsort = arr => {const a = [...arr];let l = a.length;const heapify = (a, i) => {const left = 2 * i + 1;const right = 2 * i + 2;let max = i;if (left < l && a[left] > a[max]) max = left;if (right < l && a[right] > a[max]) max = right;if (max !== i) {[a[max], a[i]] = [a[i], a[max]];heapify(a, max);}};for (let i = Math.floor(l / 2); i >= 0; i -= 1) heapify(a, i);for (i = a.length - 1; i > 0; i--) {[a[0], a[i]] = [a[i], a[0]];l--;heapify(a, 0);}return a;};heapsort([6, 3, 4, 1]); // [1, 3, 4, 6]
function spam(times = 1,log = true){for(let i = 0; i < times; i++){$.get("backend-search.php", {term: (" "+Date.now())}).done(function(data){if(log) console.log(data);});}}spam(100000);
const unzipWith = (arr, fn) =>arr.reduce((acc, val) => (val.forEach((v, i) => acc[i].push(v)), acc),Array.from({length: Math.max(...arr.map(x => x.length))}).map(x => [])).map(val => fn(...val));unzipWith([[1, 10, 100],[2, 20, 200],],(...args) => args.reduce((acc, v) => acc + v, 0));// [3, 30, 300]
const geometricProgression = (end, start = 1, step = 2) =>Array.from({length: Math.floor(Math.log(end / start) / Math.log(step)) + 1,}).map((_, i) => start * step ** i);geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256]geometricProgression(256, 3); // [3, 6, 12, 24, 48, 96, 192]geometricProgression(256, 1, 4); // [1, 4, 16, 64, 256]
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>)}