• Nov 19, 2022 •CodeCatch
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]
• Nov 18, 2022 •AustinLeath
// Get the text that the user has selected const getSelectedText = () => window.getSelection().toString(); getSelectedText();
• Oct 9, 2023 •Helper
0 likes • 286 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> ) }
0 likes • 1 view
//JavaScript program to swap two variables //take input from the users let a = prompt('Enter the first variable: '); let b = prompt('Enter the second variable: '); // XOR operator a = a ^ b b = a ^ b a = a ^ b console.log(`The value of a after swapping: ${a}`); console.log(`The value of b after swapping: ${b}`);
• Jan 26, 2023 •AustinLeath
0 likes • 5 views
function printHeap(heap, index, level) { if (index >= heap.length) { return; } console.log(" ".repeat(level) + heap[index]); printHeap(heap, 2 * index + 1, level + 1); printHeap(heap, 2 * index + 2, level + 1); } //You can call this function by passing in the heap array and the index of the root node, which is typically 0, and level = 0. let heap = [3, 8, 7, 15, 17, 30, 35, 2, 4, 5, 9]; printHeap(heap,0,0)
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